我正在学习线程,我发现了一些简单的例子。
我希望做的是创建 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;
}
}