临界区 事件 互斥对象等多线程编程基础

1、临界区

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

/*
  临界区对象
  用户使用某个线程访问共享资源时,必须是代码段独享该资源,不允许其他线程访问该资源
*/


DWORD WINAPI myfunc1(
    LPVOID lpParameter
);
DWORD WINAPI myfunc2(
    LPVOID lpParameter
);

CRITICAL_SECTION Section;//临界区对象
static int a1 = 0;//全局变量

int main(){
    HANDLE h1,h2;

    InitializeCriticalSection(&Section);
    ::Sleep(10000);

    h1 = CreateThread(NULL,0,myfunc1,NULL,0,NULL);
    cout<<"线程1开始运行!"<<endl;
    h2 = CreateThread(NULL,0,myfunc2,NULL,0,NULL);
    cout<<"线程2开始运行!"<<endl;

    //释放临界区资源对象
    ::CloseHandle(h1);
    ::CloseHandle(h2);

    if (getchar()=='q'){
        DeleteCriticalSection(&Section);
        return 0;
    }else
    {
        ::Sleep(100);
        }
}
/*
DWORD WINAPI myfunc1( LPVOID lpParameter ){
    cout<<"线程1正在运行!"<<endl;
    return 0;
}
DWORD WINAPI myfunc2(LPVOID lpParameter){
    cout<<"线程2正在运行!"<<endl;
    return 0;
}
*/

DWORD WINAPI myfunc1( LPVOID lpParameter ){
    while(1){
        EnterCriticalSection(&Section);
        a1++;
        if (a1 < 10000)
        {
            ::Sleep(1000);
            cout<<"Thread 1 was counting "<<a1<<endl;
            LeaveCriticalSection(&Section);
        }else{
            LeaveCriticalSection(&Section);
            break;
        }
    }
    return 0;
}

DWORD WINAPI myfunc2( LPVOID lpParameter ){
    while(1){
        EnterCriticalSection(&Section);
        a1++;
        if (a1 < 10000)
        {
            ::Sleep(1000);
            cout<<"Thread 2 was counting "<<a1<<endl;
            LeaveCriticalSection(&Section);
        }else{
            LeaveCriticalSection(&Section);
            break;
        }
    }
    return 0;
}

 

2、事件对象

#include <Windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;

DWORD WINAPI myfunc1(LPVOID lpParam);
DWORD WINAPI myfunc2(LPVOID lpParam);

/*
  事件对象
  用户再程序中使用内核对象的有无信号状态实现线程的同步

**/
HANDLE hEvent;
int a = 0;

int main()
{
    HANDLE h1,h2;
    hEvent = ::CreateEvent(NULL,FALSE,FALSE,NULL);
    ::SetEvent(hEvent);

    h1 = ::CreateThread(NULL,0,myfunc1,NULL,0,NULL);
    cout<<"线程1开始运行!"<<endl;
    h2 = ::CreateThread(NULL,0,myfunc2,NULL,0,NULL);
    cout<<"线程2开始运行!"<<endl;
    ::CloseHandle(h1);
    ::CloseHandle(h2);
    
    ::Sleep(10000);
    //cin.get();
    return 0;
}

DWORD WINAPI myfunc1(LPVOID lpParam)
{
    while (TRUE)
    {
        ::WaitForSingleObject(hEvent,INFINITE);//请求事件对象
        ::ResetEvent(hEvent);//设置事件对象为无信号状态
        if(a < 10000)
        {
             a += 1;
             ::Sleep(1000);
             cout<<"线程1:"<<a<<endl;
             ::SetEvent(hEvent);
        }else
        {
            ::SetEvent(hEvent);
            break;
        }
    }
    return 0;
}
DWORD WINAPI myfunc2(LPVOID lpParam)
{
    while (true)
    {
        ::WaitForSingleObject(hEvent,INFINITE);
        ::ResetEvent(hEvent);
        if(a < 10000)
        {
            a += 1;
            ::Sleep(1000);
            cout<<"线程2:"<<a<<endl;
            ::SetEvent(hEvent);
        }else
        {
            ::SetEvent(hEvent);
            break;
        }
    }
    return 0;
}

3、互斥对象

#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

/*
  互斥对象
  互斥对象也可以用于实现线程同步,同时可以在进程之间使用
*/


DWORD WINAPI myfunc1(LPVOID lpParam);
DWORD WINAPI myfunc2(LPVOID lpParam);

HANDLE hMutex;
int a = 0;

int main()
{
    hMutex = ::CreateMutex(NULL,FALSE,NULL);

    HANDLE h1,h2;
    h1 = ::CreateThread(NULL,0,myfunc1,NULL,0,NULL);
    cout<<"线程1开始运行!"<<endl;
    h2 = ::CreateThread(NULL,0,myfunc2,NULL,0,NULL);
    cout<<"线程2开始运行!"<<endl;
    ::CloseHandle(h1);
    ::CloseHandle(h2);
    
    ::Sleep(10000);
    return 0;
}

DWORD WINAPI myfunc1(LPVOID lpParam)
{
    while (TRUE)
    {
        ::WaitForSingleObject(hMutex,INFINITE);
        if(a < 10000)
        {
            a += 1;
            ::Sleep(1000);            
            cout<<"线程1:"<<a<<endl;
            ::ReleaseMutex(hMutex);
        }else
        {
            ::ReleaseMutex(hMutex);
            break;
        }
    }

    return 0;
}
DWORD WINAPI myfunc2(LPVOID lpParam)
{
    while (TRUE)
    {
        ::WaitForSingleObject(hMutex,INFINITE);
        if(a < 10000)
        {
            a += 1;
            ::Sleep(1000);            
            cout<<"线程1:"<<a<<endl;
            ::ReleaseMutex(hMutex);
        }else
        {
            ::ReleaseMutex(hMutex);
            break;
        }
    }

    return 0;
}

3、互斥对象

#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

/*
  互斥对象
  互斥对象也可以用于实现线程同步,同时可以在进程之间使用
*/


DWORD WINAPI myfunc1(LPVOID lpParam);
DWORD WINAPI myfunc2(LPVOID lpParam);

HANDLE hMutex;
int a = 0;

int main()
{
    hMutex = ::CreateMutex(NULL,FALSE,NULL);

    HANDLE h1,h2;
    h1 = ::CreateThread(NULL,0,myfunc1,NULL,0,NULL);
    cout<<"线程1开始运行!"<<endl;
    h2 = ::CreateThread(NULL,0,myfunc2,NULL,0,NULL);
    cout<<"线程2开始运行!"<<endl;
    ::CloseHandle(h1);
    ::CloseHandle(h2);
    
    ::Sleep(10000);
    return 0;
}

DWORD WINAPI myfunc1(LPVOID lpParam)
{
    while (TRUE)
    {
        ::WaitForSingleObject(hMutex,INFINITE);
        if(a < 10000)
        {
            a += 1;
            ::Sleep(1000);            
            cout<<"线程1:"<<a<<endl;
            ::ReleaseMutex(hMutex);
        }else
        {
            ::ReleaseMutex(hMutex);
            break;
        }
    }

    return 0;
}
DWORD WINAPI myfunc2(LPVOID lpParam)
{
    while (TRUE)
    {
        ::WaitForSingleObject(hMutex,INFINITE);
        if(a < 10000)
        {
            a += 1;
            ::Sleep(1000);            
            cout<<"线程1:"<<a<<endl;
            ::ReleaseMutex(hMutex);
        }else
        {
            ::ReleaseMutex(hMutex);
            break;
        }
    }

    return 0;
}
posted @ 2015-12-31 16:23  hongjack  阅读(287)  评论(0编辑  收藏  举报