这是在一个数组上执行 4 个线程,然后将该数组分配给累积计数吗?我相信是的。我知道 rand() 不是线程安全的,一旦我知道逻辑是正确的,我就会改变它。
这是一些关于通过引用线程传递数组 然后 是c++多线程数组的问题的一些建议的集合
我知道这不是在练习最佳实践方法,但我只是试图让它启动并运行。
我想我让它运行正确,不得不将一个 for 计数器变量从 x 更改为 p,不知道为什么......还将 fHolder 移出 for 循环。
我的 fholder 总是结果为 0,我不知道为什么。我检查了计数器的值...
#include <process.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <time.h>
//#include <thread>
using namespace std;
void myThread0 (void *dummy );
void myThread1 (void *dummy );
void myThread2 (void *dummy );
void myThread3 (void *dummy );
//only needed for shared variables
//CRITICAL_SECTION cs1,cs2,cs3,cs4; // global
int main()
{
//InitializeCriticalSection(&cs1);
//InitializeCriticalSection(&cs2);
//InitializeCriticalSection(&cs3);
//InitializeCriticalSection(&cs4);
ofstream myfile;
myfile.open ("coinToss.csv");
int rNum;
long numRuns;
long count = 0;
int divisor = 1;
float fHolder = 0;
long counter = 0;
float percent = 0.0;
//?
unsigned threadID;
//HANDLE hThread;
HANDLE hThread[4];
const int size = 100000;
int array[size];
srand ( time(NULL) );
printf ("Runs (uses multiple of 100,000) ");
cin >> numRuns;
for (int a = 0; a < numRuns; a++)
{
hThread[0] = (HANDLE)_beginthread( myThread0, 0, (void*)(array) );
hThread[1] = (HANDLE)_beginthread( myThread1, 0, (void*)(array) );
hThread[2] = (HANDLE)_beginthread( myThread2, 0, (void*)(array) );
hThread[3] = (HANDLE)_beginthread( myThread3, 0, (void*)(array) );
//waits for threads to finish before continuing
WaitForMultipleObjects(4, hThread, TRUE, INFINITE);
//closes handles I guess?
CloseHandle( hThread[0] );
CloseHandle( hThread[1] );
CloseHandle( hThread[2] );
CloseHandle( hThread[3] );
//dump array into calculations
//average array into fHolder
for (int p = 0; p < size; p++)
{
counter += array[p] == 2 ? 1 : -1;
//cout << counter << endl;
//cout << count << endl;
//cout << p << endl;
counter = count + counter;
//divide into an exportable value
//divides by 1,000,000, because each thread handles 250,000
//cout << "Value " << x << ": " << array[x] << endl;
}
fHolder = counter / size;
cout << "Final Count: " << counter << endl;
cout << "fHolder: " << fHolder << endl;
myfile << fHolder << endl;
}
}
void myThread0 (void *param)
{
//EnterCriticalSection(&cs1); //aquire the critical section object
int *i = (int *)param;
for (int x = 0; x < 25000; x++)
{
i[x] = rand() % 2 + 1;
//cout << i[x] << endl;
}
//LeaveCriticalSection(&cs1); // release the critical section object
}
void myThread1 (void *param)
{
//EnterCriticalSection(&cs2); //aquire the critical section object
int *i = (int *)param;
for (int x = 25000; x < 50000; x++)
{
//param[x] = rand() % 2 + 1;
i[x] = rand() % 2 + 1;
//cout << i[x] << endl;
}
//LeaveCriticalSection(&cs2); // release the critical section object
}
void myThread2 (void *param)
{
//EnterCriticalSection(&cs3); //aquire the critical section object
int *i = (int *)param;
for (int x = 50000; x < 75000; x++)
{
i[x] = rand() % 2 + 1;
//cout << i[x] << endl;
}
//LeaveCriticalSection(&cs3); // release the critical section object
}
void myThread3 (void *param)
{
//EnterCriticalSection(&cs4); //aquire the critical section object
int *i = (int *)param;
for (int x = 75000; x < 100000; x++)
{
i[x] = rand() % 2 + 1;
//cout << i[x] << endl;
}
//LeaveCriticalSection(&cs4); // release the critical section object
}