使用关键代码段实现线程同步
2013-10-24 20:33
239 查看
关键代码段在很多硬件系统(比如Linux,ucOS等等)也存在,只是系统开发用于来保护现场,根据上Demo,更改如下:
#include<windows.h>
#include<iostream.h>
DWORD WINAPI FuncThreadone(LPVOID lpParameter);
DWORD WINAPI FuncThreadTwo(LPVOID lpParameter);
int index=0;
int tickets=100;
HANDLE hMutex;
HANDLE m_hEvent;
CRITICAL_SECTION g_cs;
void main(){
HANDLE hThread1;
HANDLE hThread2;
hMutex=CreateMutex(NULL,0,NULL);
m_hEvent=CreateEvent(NULL,FALSE,TRUE,NULL);
//SetEvent(m_hEvent);
hThread1=CreateThread(NULL,0,FuncThreadone,NULL,0,NULL);
hThread2=CreateThread(NULL,0,FuncThreadTwo,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
cout<<"main thread is running"<<endl;
//Sleep(10);
InitializeCriticalSection(&g_cs);
Sleep(4000);
DeleteCriticalSection(&g_cs);
//CloseHandle(m_hEvent);
}
DWORD WINAPI FuncThreadone(LPVOID lpParameter){
//cout<<"thread one is running !"<<endl;
while(1){
//WaitForSingleObject(hMutex,INFINITE);
//WaitForSingleObject(m_hEvent,INFINITE);
//ResetEvent(m_hEvent);
EnterCriticalSection(&g_cs);
if(tickets>0){
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
//SetEvent(m_hEvent);
LeaveCriticalSection(&g_cs);
}else{
//SetEvent(m_hEvent);
LeaveCriticalSection(&g_cs);
break;
}
//ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI FuncThreadTwo(LPVOID lpParameter){
//cout<<"thread two is running !"<<endl;
while(1){
//WaitForSingleObject(hMutex,INFINITE);
//WaitForSingleObject(m_hEvent,INFINITE);
//ResetEvent(m_hEvent);
EnterCriticalSection(&g_cs);
if(tickets>0){
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
LeaveCriticalSection(&g_cs);
//SetEvent(m_hEvent);
}else{
//SetEvent(m_hEvent);
LeaveCriticalSection(&g_cs);
break;
}
//ReleaseMutex(hMutex);
}
return 0;
}
#include<windows.h>
#include<iostream.h>
DWORD WINAPI FuncThreadone(LPVOID lpParameter);
DWORD WINAPI FuncThreadTwo(LPVOID lpParameter);
int index=0;
int tickets=100;
HANDLE hMutex;
HANDLE m_hEvent;
CRITICAL_SECTION g_cs;
void main(){
HANDLE hThread1;
HANDLE hThread2;
hMutex=CreateMutex(NULL,0,NULL);
m_hEvent=CreateEvent(NULL,FALSE,TRUE,NULL);
//SetEvent(m_hEvent);
hThread1=CreateThread(NULL,0,FuncThreadone,NULL,0,NULL);
hThread2=CreateThread(NULL,0,FuncThreadTwo,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
cout<<"main thread is running"<<endl;
//Sleep(10);
InitializeCriticalSection(&g_cs);
Sleep(4000);
DeleteCriticalSection(&g_cs);
//CloseHandle(m_hEvent);
}
DWORD WINAPI FuncThreadone(LPVOID lpParameter){
//cout<<"thread one is running !"<<endl;
while(1){
//WaitForSingleObject(hMutex,INFINITE);
//WaitForSingleObject(m_hEvent,INFINITE);
//ResetEvent(m_hEvent);
EnterCriticalSection(&g_cs);
if(tickets>0){
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
//SetEvent(m_hEvent);
LeaveCriticalSection(&g_cs);
}else{
//SetEvent(m_hEvent);
LeaveCriticalSection(&g_cs);
break;
}
//ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI FuncThreadTwo(LPVOID lpParameter){
//cout<<"thread two is running !"<<endl;
while(1){
//WaitForSingleObject(hMutex,INFINITE);
//WaitForSingleObject(m_hEvent,INFINITE);
//ResetEvent(m_hEvent);
EnterCriticalSection(&g_cs);
if(tickets>0){
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
LeaveCriticalSection(&g_cs);
//SetEvent(m_hEvent);
}else{
//SetEvent(m_hEvent);
LeaveCriticalSection(&g_cs);
break;
}
//ReleaseMutex(hMutex);
}
return 0;
}
相关文章推荐
- 使用关键代码段实现线程同步
- 使用关键代码段实现线程同步
- VC++多线程,互斥对象,事件对象,关键代码段实现线程同步
- 利用关键代码段实现线程同步
- windows 使用关键段和条件变量实现的生产者和消费者线程同步
- 利用关键代码段实现线程同步
- windows多线程系列004_利用关键代码段实现线程同步
- C++多线程编程例子 Demo 关键代码段 临界区 实现线程同步
- 利用关键代码段实现线程同步
- 线程同步使用方法:CMutex_CEvent_关键代码段
- 多线程编程Demo[关键代码段[临界区]实现线程同步]
- [VC网络编程笔记-使用API函数操作互斥对象实现线程同步
- 线程同步--关键代码段(二)
- 使用log4cplus实现的分目录存放日志服务器完整工程+关键客户端代码
- 使用事件对象实现线程同步
- 使用Win32 API实现生产者消费者线程同步
- 线程同步 关键代码段 CRITICAL_SECTION 用户模式同步对象 InitializeCriticalSection
- 基于.net mvc的校友录(六、codefirst的使用以及班级模块的关键部分实现)
- 使用信标对象实现线程同步
- C++使用信号量对象实现线程同步