这是在一个数组上做4个线程,然后将该数组分配给累积计数?我相信它是。我知道rand()不是线程安全的,一旦我知道逻辑是正确的,我就会改变它。
这是对一些建议的恭维 Problems passing array by reference to threads 然后 c++ multithread array
我知道这不是练习最佳练习方法,但我只是想让它运行起来。
我认为我让它正常运行,不得不将for for counter变量从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
}
答案 0 :(得分:1)
_beginthread
存在问题,您应该使用_beginthreadex
。阅读这个新功能的文档,了解如何使用它。WaitForMultipleObjects
时应该有4个而不是1个,并且应该有四个调用CloseHandle
来关闭所有四个句柄。cout << i[x] << endl;
上同步,因此运行速度比必要的慢。 cout << i[x] << endl;
的问题在于它不是原子操作,它由四个线程同时运行。一个线程可能在另一个线程的i[x]
和endl
的着作之间写下它的整行,这将产生不希望的结果。我会完全从线程中删除cout
。counter += array[x] == 2 ? 1 : -1;
。此处不需要变量count
。param
作为指向为此线程指定的数组部分开头的指针。这将允许您只有一个由四个线程运行的函数,其中所有四个线程的参数值都不同。