C++-用关键段实现线程同步
下面是一段用关键段实现线程同步的例子:
//关键段测试代码
#include <iostream>
#include <windows.h>
using namespace std;
CRITICAL_SECTION g_cs;//创建CRITICAL_SECTION结构全局变量
int g_x = 50;
//线程函数1
DWORD WINAPI ThreadFunc1(LPVOID lpParam)
{
while(TRUE)
{
EnterCriticalSection(&g_cs);
cout << "Thread1 get the access to resource" << endl;
if(g_x > 25)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<g_x--<<endl;
}
else
break;
LeaveCriticalSection(&g_cs);
}
return 0;
}
//线程函数2
DWORD WINAPI ThreadFunc2(LPVOID lpParameter)
{
while(TRUE)
{
EnterCriticalSection(&g_cs);
cout << "Thread2 get the access to resource" << endl;
if(g_x > 0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<g_x--<<endl;
}
else
break;
LeaveCriticalSection(&g_cs);
}
return 0;
}
int main()
{
HANDLE hThread1;
HANDLE hThread2;
InitializeCriticalSection(&g_cs);
hThread1=CreateThread(NULL,0,ThreadFunc1,NULL,0,NULL);
hThread2=CreateThread(NULL,0,ThreadFunc2,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
Sleep(4000);
DeleteCriticalSection(&g_cs);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
线程1 break后没有LeaveCriticalSection,导致了问题。
两个线程中都添加类似下面的处理
//线程函数1
DWORD WINAPI ThreadFunc1(LPVOID lpParam)
{
while(TRUE)
{
EnterCriticalSection(&g_cs);
cout << "Thread1 get the access to resource" << endl;
if(g_x > 25)
{
Sleep(10);
cout<<"thread1 sell ticket : "<<g_x--<<endl;
}
else
{
LeaveCriticalSection(&g_cs);//add
break;
}
LeaveCriticalSection(&g_cs);
}
cout << "Thread1 exit" << endl;
return 0;
}
哥哥:试着修改成这个哈,首先一点: sleep() 是要释放资源的,不然在你睡眠过程中你还是将资源锁住的哈。
cout<<"thread2 sell ticket : "<<g_x--<<endl;
LeaveCriticalSection(&g_cs);
Sleep(1);
你的代码存在几个细节问题:
1.睡眠的时候占用了资源,应该在睡眠前释放资源
2.else判断后跳出循环存在资源未释放的危险,造成死锁
3.CloseHandle(hThread1) 函数并没有关闭线程,DeleteCriticalSection(&g_cs)此时释放资源可能会造成正在运行的线程出现资源访问错误(主进程调用sleep进行睡眠是你程序没出错的原因,但这样并不可靠)。
下面是我修改过后的代码,你可以试试:
#include <iostream>
#include <windows.h>
using namespace std;
CRITICAL_SECTION g_cs;//创建CRITICAL_SECTION结构全局变量
int g_x = 50;
//线程函数1
DWORD WINAPI ThreadFunc1(LPVOID lpParam)
{
while(TRUE)
{
EnterCriticalSection(&g_cs);
cout << "Thread1 get the access to resource" << endl;
if(g_x > 25)
{
cout<<"thread1 sell ticket : "<<g_x--<<endl;
}
else
break;
LeaveCriticalSection(&g_cs);
Sleep(1);
}
LeaveCriticalSection(&g_cs);
return 0;
}
//线程函数2
DWORD WINAPI ThreadFunc2(LPVOID lpParameter)
{
while(TRUE)
{
EnterCriticalSection(&g_cs);
cout << "Thread2 get the access to resource" << endl;
if(g_x > 0)
{
cout<<"thread2 sell ticket : "<<g_x--<<endl;
}
else
break;
LeaveCriticalSection(&g_cs);
Sleep(1);
}
LeaveCriticalSection(&g_cs);
return 0;
}
int main()
{
HANDLE hThread1;
HANDLE hThread2;
InitializeCriticalSection(&g_cs);
hThread1=CreateThread(NULL,0,ThreadFunc1,NULL,0,NULL);
hThread2=CreateThread(NULL,0,ThreadFunc2,NULL,0,NULL);
WaitForSingleObject(hThread1,INFINITE);
WaitForSingleObject(hThread2,INFINITE);
CloseHandle(hThread1);
CloseHandle(hThread2);
DeleteCriticalSection(&g_cs);
cout<<"Press Enter to end"<<endl;
getchar();
return 0;
}