windows thread的简单使用

由于主要使用pthread-win32,所以对windows自带的线程没那么熟练,写个例子练下手:

#include #include #include #include #include #pragma comment(lib,"MSVCRT.LIB") HANDLE event; HANDLE mutex; unsigned int __stdcall func(void *) { DWORD ret = WaitForSingleObject(mutex,5000); if (ret == WAIT_OBJECT_0) { while (true) { ret = WaitForSingleObject(event,1000); if (ret == WAIT_OBJECT_0) { printf("child thread is visiting buffer./n"); _sleep(100); } SetEvent(event); } } return 1; } void main() { mutex = CreateMutex(NULL,FALSE,"MUTEX"); event = CreateEvent(NULL,FALSE,TRUE,"EVNET1"); _beginthreadex(NULL,0,func,NULL,0,NULL); _sleep(1000); while (true) { DWORD ret = WaitForSingleObject(event,1000); if (ret == WAIT_OBJECT_0) { printf("parent thread is visiting buffer./n"); _sleep(100); } SetEvent(event); } } 

 

。。。

你可能感兴趣的:(C/C++)