我正在学习线程,并找到了一些简单的例子。
我的希望是创建5个线程,每个线程将随机数分配给包含20个int的数组。最后再创建另外5个线程将这些内容重构成一个更大的包含100个int的数组。
这是我之前尝试过的一些代码。我原本希望能通过引用传递数组,但没有成功。
如果您有任何想法,请记住,我完全不懂线程。
#include <process.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <time.h>
//#include <thread>
using namespace std;
void myThread (void *dummy );
void myThread2 (void *dummy );
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;
int array1[1000000];
int array2[1000000];
srand ( time(NULL) );
printf ("Runs (use multiple of 10)? ");
cin >> numRuns;
for (int i = 0; i < numRuns; i++)
{
_beginthread( myThread, 0, (void *) (array1) );
_beginthread( myThread2, 0, (void *) (array2) );
}
}
void myThread (void *param )
{
int i = *(int *)param;
for (int x = 0; x < 1000000; x++)
{
//param[x] = rand() % 2 + 1;
i[x] = rand() % 2 + 1;
}
}
void myThread2 (void *param )
{
int i[1000000] = *(int *)param;
for (int = 0; x < 1000000; x++)
{
i[x] = rand() % 2 + 1;
}
}