读书人

线程同步,该如何解决

发布时间: 2012-09-11 10:49:03 作者: rapoo

线程同步


#include <iostream>
#include <Windows.h>
using namespace std;


DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);

int index=50;
//HANDLE gMutex;
//HANDLE gEvent;
CRITICAL_SECTION gCs;
int main()
{
HANDLE h1,h2;
//gMutex=CreateMutex(NULL,FALSE,NULL);
//gEvent=CreateEvent(NULL,FALSE,TRUE,NULL);
InitializeCriticalSection(&gCs);
h1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
h2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);

//ResumeThread(h2);
CloseHandle(h1); //系统会递减内核对象的使用计数,否则一直不会释放
CloseHandle(h2);

Sleep(4000);
//CloseHandle(gMutex);
//CloseHandle(gEvent); //经测试,关闭事件对象和互斥对象必须最后;
DeleteCriticalSection(&gCs);
return 0;
}



DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while(TRUE)
{
//WaitForSingleObject(gEvent,INFINITE);
EnterCriticalSection(&gCs);
Sleep(1);
if (index>0)
{
cout<<"Thread 1 selling the "<<index<<" tickets"<<endl;
index--;
}
else
break;
//SetEvent(gEvent);
//ReleaseMutex(gMutex);
LeaveCriticalSection(&gCs);
}
return 0;
}

DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while(TRUE)
{
//WaitForSingleObject(gEvent,INFINITE);
EnterCriticalSection(&gCs);
Sleep(1);
if (index>0)
{
cout<<"Thread 2 selling the "<<index<<" tickets"<<endl;
index--;
}
else
break;
//SetEvent(gEvent);
//ReleaseMutex(gMutex);
LeaveCriticalSection(&gCs);
}

return 0;
}



孙鑫的多线程同步程序,检查了几遍没有错误啊,为什么会出现只有线程 1 运行的情况呢??
Thread 1 selling the 50 tickets
Thread 1 selling the 49 tickets
Thread 1 selling the 48 tickets
Thread 1 selling the 47 tickets
Thread 1 selling the 46 tickets
Thread 1 selling the 45 tickets
Thread 1 selling the 44 tickets
Thread 1 selling the 43 tickets
Thread 1 selling the 42 tickets
Thread 1 selling the 41 tickets
Thread 1 selling the 40 tickets
Thread 1 selling the 39 tickets
Thread 1 selling the 38 tickets
Thread 1 selling the 37 tickets
Thread 1 selling the 36 tickets
Thread 1 selling the 35 tickets
Thread 1 selling the 34 tickets
Thread 1 selling the 33 tickets
Thread 1 selling the 32 tickets
Thread 1 selling the 31 tickets
Thread 1 selling the 30 tickets
Thread 1 selling the 29 tickets
Thread 1 selling the 28 tickets
Thread 1 selling the 27 tickets
Thread 1 selling the 26 tickets
Thread 1 selling the 25 tickets
Thread 1 selling the 24 tickets
Thread 1 selling the 23 tickets
Thread 1 selling the 22 tickets
Thread 1 selling the 21 tickets
Thread 1 selling the 20 tickets
Thread 1 selling the 19 tickets
Thread 1 selling the 18 tickets
Thread 1 selling the 17 tickets
Thread 1 selling the 16 tickets
Thread 1 selling the 15 tickets
Thread 1 selling the 14 tickets
Thread 1 selling the 13 tickets
Thread 1 selling the 12 tickets
Thread 1 selling the 11 tickets
Thread 1 selling the 10 tickets
Thread 1 selling the 9 tickets
Thread 1 selling the 8 tickets
Thread 1 selling the 7 tickets
Thread 1 selling the 6 tickets
Thread 1 selling the 5 tickets


Thread 1 selling the 4 tickets
Thread 1 selling the 3 tickets
Thread 1 selling the 2 tickets
Thread 1 selling the 1 tickets



[解决办法]
交换一下:
Sleep(1);
EnterCriticalSection(&gCs);
的位置

读书人网 >C++

热点推荐