读书人

多线程 事件对象的有关问题

发布时间: 2012-08-07 14:54:48 作者: rapoo

多线程 事件对象的问题

C/C++ code
#include <iostream>#include <Windows.h>using namespace std;DWORD WINAPI Fun1Proc(LPVOID lpParameter);DWORD WINAPI Fun2Proc(LPVOID lpParameter);int tickets=100;HANDLE g_hEvent;int main(){    HANDLE hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);    HANDLE hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);    //it does not close the thread    CloseHandle(hThread1);    CloseHandle(hThread2);    //initialize the event object to be non signaled state    //do not use manual reset mode    g_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);    //set  the event object to be signaled state    SetEvent(g_hEvent);    Sleep(4000);    return 0;}DWORD WINAPI Fun1Proc(LPVOID lpParameter){    while(TRUE){            WaitForSingleObject(g_hEvent,INFINITE);//            ResetEvent(g_hEvent);            if (tickets>0)            {                cout<<"thread1 sells ticket:"<<tickets--<<endl;                }            else{                break;            }            SetEvent(g_hEvent);        }    return 0;}DWORD WINAPI Fun2Proc(LPVOID lpParameter){    while(TRUE){        WaitForSingleObject(g_hEvent,INFINITE);//        ResetEvent(g_hEvent);        if (tickets>0)        {            cout<<"thread2 sells ticket:"<<tickets--<<endl;            }        else{            break;        }        SetEvent(g_hEvent);    }    return 0;}

运行结果:
thread1 sells ticket:thread2 sells ticket:99
100
thread2 sells ticket:98
thread1 sells ticket:97
thread2 sells ticket:96
thread1 sells ticket:95
thread2 sells ticket:94
thread1 sells ticket:93
thread2 sells ticket:92
thread1 sells ticket:91
thread2 sells ticket:90
thread1 sells ticket:89
thread2 sells ticket:88
thread1 sells ticket:87
thread2 sells ticket:86
thread1 sells ticket:85
thread2 sells ticket:84
thread1 sells ticket:83
thread2 sells ticket:82
thread1 sells ticket:81
thread2 sells ticket:80
thread1 sells ticket:79
thread2 sells ticket:78
thread1 sells ticket:77
thread2 sells ticket:76
thread1 sells ticket:75
thread2 sells ticket:74
thread1 sells ticket:73
thread2 sells ticket:72
thread1 sells ticket:71
thread2 sells ticket:70
thread1 sells ticket:69
thread2 sells ticket:68
thread1 sells ticket:67
thread2 sells ticket:66
thread1 sells ticket:65
thread2 sells ticket:64
thread1 sells ticket:63
thread2 sells ticket:62
thread1 sells ticket:61
thread2 sells ticket:60
thread1 sells ticket:59
thread2 sells ticket:58
thread1 sells ticket:57
thread2 sells ticket:56
thread1 sells ticket:55
thread2 sells ticket:54
thread1 sells ticket:53
thread2 sells ticket:52
thread1 sells ticket:51
thread2 sells ticket:50
thread1 sells ticket:49
thread2 sells ticket:48
thread1 sells ticket:47
thread2 sells ticket:46
thread1 sells ticket:45
thread2 sells ticket:44
thread1 sells ticket:43
thread2 sells ticket:42
thread1 sells ticket:41
thread2 sells ticket:40
thread1 sells ticket:39
thread2 sells ticket:38
thread1 sells ticket:37
thread2 sells ticket:36
thread1 sells ticket:35
thread2 sells ticket:34
thread1 sells ticket:33
thread2 sells ticket:32
thread1 sells ticket:31
thread2 sells ticket:30
thread1 sells ticket:29
thread2 sells ticket:28
thread1 sells ticket:27
thread2 sells ticket:26
thread1 sells ticket:25
thread2 sells ticket:24
thread1 sells ticket:23
thread2 sells ticket:22
thread1 sells ticket:21
thread2 sells ticket:20
thread1 sells ticket:19
thread2 sells ticket:18
thread1 sells ticket:17
thread2 sells ticket:16


thread1 sells ticket:15
thread2 sells ticket:14
thread1 sells ticket:13
thread2 sells ticket:12
thread1 sells ticket:11
thread2 sells ticket:10
thread1 sells ticket:9
thread2 sells ticket:8
thread1 sells ticket:7
thread2 sells ticket:6
thread1 sells ticket:5
thread2 sells ticket:4
thread1 sells ticket:3
thread2 sells ticket:2
thread1 sells ticket:1


已经用了事件对象来阻塞了,为什么第一行的输出会是这个样子呢?.

[解决办法]
g_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
把这句放到创建线程前面。不然线程先跑起来事件还没创建好,一个无效的事件句柄,waitforsingleobkect会马上返回的,跟木有同步一样

读书人网 >VC/MFC

热点推荐