windows 线程同步的4种方法

来源:互联网 发布:java敲代码 编辑:程序博客网 时间:2024/06/05 09:46

1.     互斥信号量

   #include <windows.h>
#include <iostream>
using namespace std;

DWORD WINAPI Fun1Proc(LPVOID param);
DWORD WINAPI Fun2Proc(LPVOID param);

int time 0;
HANDLE Mutex;

void main()
{
    HANDLE thread1,thread2;
    thread1 CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
    thread2 CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
    CloseHandle(thread1);
    CloseHandle(thread2);
   
 Mutex CreateMutex(NULL,FALSE,NULL);
    cout << "
运行主线程!" << endl;
    Sleep(4000);
}

DWORD WINAPI Fun1Proc(LPVOID param)
{
    
while(1) {
        
WaitForSingleObject(Mutex,INFINITE);
        if(time <= 20) {
            Sleep(1);
            cout << "
线程1运行第<< time++ << "<< endl;
        }
        
else
            
break;
      
  ReleaseMutex(Mutex);
    }
    
return 0;
}

DWORD WINAPI Fun2Proc(LPVOID param)
{
    
while(1) {
       
 WaitForSingleObject(Mutex,INFINITE);
        if(time <= 20) {
            Sleep(1);
            cout << "
线程2运行第<< time++ << "<< endl;
        }
        
else
            
break;
      
  ReleaseMutex(Mutex);
    }
    
return 0;
}

2. 基于事件的同步方法

#include <windows.h>
#include <iostream>
using namespace std;

DWORD WINAPI Fun1Proc(LPVOID param);
DWORD WINAPI Fun2Proc(LPVOID Param);

int time;
HANDLE events;

void main()
{
    HANDLE thread1,thread2;
    thread1 CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
    thread2 CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
    CloseHandle(thread1);
    CloseHandle(thread2);
    
events CreateEvent(NULL,FALSE,FALSE,NULL);
    SetEvent(events);
    Sleep(4000);
    CloseHandle(events);
}
DWORD WINAPI Fun1Proc(LPVOID param)
{
    
while(1) {
        
WaitForSingleObject(events,INFINITE);
        if(time <= 20) {
            Sleep(1);
            cout << "
线程1运行第<< time++ << "<< endl;
        }
        
else
            
break;
       
 SetEvent(events);
    }
    
return 0;
}

DWORD WINAPI Fun2Proc(LPVOID param)
{
    
while(1) {
      
  WaitForSingleObject(events,INFINITE);
        if(time <= 20) {
            Sleep(1);
            cout << "
线程2运行第<< time++ << "<< endl;
        }
        
else
            
break;
        
SetEvent(events);
    }
    
return 0;
}

3.基于临界区的进程同步方法

#include <windows.h>
#include <iostream>
using namespace std;

DWORD WINAPI Fun1Proc(LPVOID param);
DWORD WINAPI Fun2Proc(LPVOID param);

int time 0;
CRITICAL_SECTION critical;

void main()
{
    HANDLE thread1,thread2;
    thread1 CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
    thread2 CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
    CloseHandle(thread1);
    CloseHandle(thread2);

    
InitializeCriticalSection(&critical);
    Sleep(4000);
  
  DeleteCriticalSection(&critical);
}

DWORD WINAPI Fun1Proc(LPVOID param)
{
    
while(1)
    
 {
       
 EnterCriticalSection(&critical);
        if(time <= 20)
        
 {
            Sleep(1);
            cout << "
子线程1<< time ++ << ""<< endl;
        }
        
else
            
break;
        
LeaveCriticalSection(&critical);
    }
    
return 0;
}

DWORD WINAPI Fun2Proc(LPVOID param)
{
    
while(1)
    
 {
       
 EnterCriticalSection(&critical);
        if(time <= 20)
        
 {
            Sleep(1);
            cout << "
子线程2<< time ++ << "<< endl;
        }
        
else
            
break;
      
  LeaveCriticalSection(&critical);
    }
    
return 0;
}

4. 基于信号量的方法

#include <windows.h>
#include <iostream>
using namespace std;

DWORD WINAPI Fun1Proc(LPVOID param);
DWORD WINAPI Fun2Proc(LPVOID param);

int time 0;
HANDLE sema;

void main()

0 0
原创粉丝点击