c ++多线程数组 [英] c++ multithread array
问题描述
我在做有趣的事,试图学习多线程
通过引用线程传递数组的问题
但Arno指出,我通过process.h的线程不会是多线程的。 p>
我希望做的是,我有一个数组100(或10,000,不是真的重要,我不认为),并分配任务的值到每个线程。例如,4个线程=每个要分配的线程250个值。
然后我可以使用这个填充数组进一步计算。
这里是一些我正在工作的代码(不起作用)
#include< process.h>
#include< windows.h>
#include< iostream>
#include< fstream>
#include< time.h>
//#include< thread>
using namespace std;
void myThread(void * dummy);
CRITICAL_SECTION cs1,cs2; // global
int main()
{
ofstream myfile;
myfile.open(coinToss.csv);
int rNum;
long numRuns;
long count = 0;
int divisor = 1;
float holder = 0;
int counter = 0;
float percent = 0.0;
HANDLE hThread [1000];
int array [10000];
srand(time(NULL));
printf(Runs(use multiple of 10)?);
cin>> numRuns;
for(int i = 0; i {
// _ beginthread(myThread,0,(void *)(array1)
// ???
// hThread [i * 2] = _beginthread(myThread,0,(void *)(array1));
hThread [i * 2] = _beginthread(myThread,0,(void *)(array));
}
// WaitForMultipleObjects(numRuns * 2,hThread,TRUE,INFINITE);
WaitForMultipleObjects(numRuns,hThread,TRUE,INFINITE);
}
void myThread(void * param)
{
//感谢stockoverflow
// http:// stackoverflow .com / questions / 12801862 / problems-passing-array-by-reference-to-threads
int * i =(int *)param;
for(int x = 0; x <1000000; x ++)
{
// param [x] = rand()%2 + 1;
i [x] = rand()%2 + 1;
}
}
t工作?
对于初学者,使用_beginthreadex而不是_beginthread,它关闭正常跳动的线程句柄。如果在开始WFMO之前关闭线程句柄,它将很可能立即断开,因为一个或多个句柄将是无效的。
其次,你的句柄列表中的i * 2是什么?
第三,WFMO的最大等待列表长度为64个线程,所以你的一千线程的列表将确保puke一旦你达到65或更多..你只是可能想要考虑限制这个上限。实际值是MAX_WAIT_OBJECTS(或接近,我不记得确切)。
这是所有之前,我们甚至得到的数组的保护试图分享。
i'm doing something for fun, trying to learn multithreading Problems passing array by reference to threads
but Arno pointed out that my threading via process.h wasn't going to be multi-threaded.
What I'm hoping to do is something where I have an array of 100 (or 10,000, doesn't really matter I don't think), and split up the assignment of values to each thread. Example, 4 threads = 250 values per thread to be assigned.
Then I can use this filled array for further calculations.
Here's some code I was working on (which doesn't work)
#include <process.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <time.h>
//#include <thread>
using namespace std;
void myThread (void *dummy );
CRITICAL_SECTION cs1,cs2; // global
int main()
{
ofstream myfile;
myfile.open ("coinToss.csv");
int rNum;
long numRuns;
long count = 0;
int divisor = 1;
float holder = 0;
int counter = 0;
float percent = 0.0;
HANDLE hThread[1000];
int array[10000];
srand ( time(NULL) );
printf ("Runs (use multiple of 10)? ");
cin >> numRuns;
for (int i = 0; i < numRuns; i++)
{
//_beginthread( myThread, 0, (void *) (array1) );
//???
//hThread[i * 2] = _beginthread( myThread, 0, (void *) (array1) );
hThread[i*2] = _beginthread( myThread, 0, (void *) (array) );
}
//WaitForMultipleObjects(numRuns * 2, hThread, TRUE, INFINITE);
WaitForMultipleObjects(numRuns, hThread, TRUE, INFINITE);
}
void myThread (void *param )
{
//thanks goes to stockoverflow
//http://stackoverflow.com/questions/12801862/problems-passing-array-by-reference-to-threads
int *i = (int *)param;
for (int x = 0; x < 1000000; x++)
{
//param[x] = rand() % 2 + 1;
i[x] = rand() % 2 + 1;
}
}
Can anyone explain why it isn't working?
For starters, use _beginthreadex rather than _beginthread, which closes the thread handle on normal run-out. if the thread handle is closed before you begin that WFMO it will likely break immediately since one or more of the handles will be invalid.
Secondly whats with the i*2 on your handle list ? Sending a list of handle to WFMO with every other handle NULL is likely going to error immediately.
Third, WFMO has a maximum wait-list length of 64 threads, so your list of a thousand threads is going to guaranteedly puke as soon as you reach 65 or more.. You just might want to consider limiting that ceiling. The actual value is MAX_WAIT_OBJECTS (or close to that, i can't recall exactly).
And thats all before we even get to the protection of the array you're trying to share.
这篇关于c ++多线程数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!