#include <iostream>
#include <windows.h>
using namespace std;
HANDLE hMutex = NULL;//互斥量
//线程函数
DWORD WINAPI Fun(LPVOID lpParamter)
{
for (int i = 0; i < 10; i++)
{
//请求一个互斥量锁
WaitForSingleObject(hMutex, INFINITE);
cout << "线程显示" << endl;
Sleep(1000);
//释放互斥量锁
ReleaseMutex(hMutex);
}
return 0L;//表示返回的是long型的0
}
int main()
{
//创建一个子线程
HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
hMutex = CreateMutex(NULL, FALSE, "screen");
//关闭线程
CloseHandle(hThread);
//主线程的执行路径
for (int i = 0; i < 10; i++)
{
//请求获得一个互斥量锁
WaitForSingleObject(hMutex, INFINITE);
cout << "主线程显示" << endl;
Sleep(100);
//释放互斥量锁
ReleaseMutex(hMutex);
}
return 0;
}
C++多线程示例
最新推荐文章于 2022-04-26 09:48:59 发布