|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
内核模式下面的同步对象有信号灯、互斥体、事件。用户模式和内核模式基本差不多。现在列举用户模式的互斥体的使用方式:
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <process.h>
- #include <stddef.h>
- UINT WINAPI Thead1(LPVOID para)
- {
- HANDLE *phMutex = (HANDLE*)para;
- //得到互斥体
- WaitForSingleObject(phMutex,INFINITE);
- printf("Enter thead1\n");
- //Sleep(2000);
- printf("Leave thead1\n");
- ReleaseMutex(*phMutex);
- return 0;
- }
- UINT WINAPI Thead2(LPVOID para)
- {
- HANDLE *phMutex = (HANDLE*)para;
- //得到互斥体
- WaitForSingleObject(phMutex,INFINITE);
- printf("Enter thead2\n");
- //Sleep(2000);
- printf("Leave thead2\n");
- ReleaseMutex(*phMutex);
- return 0;
- }
- int main()
- {
- //创建一个同步事件
- HANDLE hMutex = CreateMutex(NULL,FALSE,NULL);
- //开启新线程,并且将同步的事件句柄传递给新县城
- HANDLE hThead1 = (HANDLE)_beginthreadex(NULL,0,Thead1,&hMutex,0,NULL);
- HANDLE hThead2 = (HANDLE)_beginthreadex(NULL,0,Thead2,&hMutex,0,NULL);
- Sleep(2000);
- return 0;
- }
复制代码
|
|