1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <Windows.h>
#include <iostream>
class CMyClass{
public:
CMyClass() : lValue(0){}
volatile long lValue;
};
DWORD WINAPI MyThreadFunction(LPVOID lpParam){
CMyClass* theClass = (CMyClass*)lpParam;
/*for(int i = 0; i < 100000; i++)
theClass->lValue += 1;*/
for(int i = 0; i < 100000; i++)
InterlockedIncrement(&theClass->lValue);
return 0L;
}
void main(){
HANDLE hThread[3];
CMyClass theClass;
for(int i = 0; i < 3; i++)
hThread[i] = CreateThread(NULL, 0, MyThreadFunction, (LPVOID)&theClass, 0,NULL);
WaitForMultipleObjects(3, hThread, TRUE, INFINITE);
for(int i = 0; i < 3; i++)
CloseHandle(hThread[i]);
std::cout << theClass.lValue << std::endl;
char c;
std::cin >> c;
} |
Partager