I'm learning threading and I've found some simple examples.
What I'm hoping to do is create 5 threads, that each assign a random number to an array of 20 int's. Then finally have another 5 threads that reconstruct this array to a larger 100 sized int.
Here's some prior code I was trying. I was hoping to be able to pass an array by reference, with no luck.
Any ideas would be appreciated, please keep in mind, I'm completely new to threads
#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;
}
}
See Question&Answers more detail:os