1. 程式人生 > >C++ 程序鎖 互斥鎖

C++ 程序鎖 互斥鎖

基本的互斥鎖用法,不解釋直接上程式碼

<span style="font-size:18px;">#include <windows.h>
#include <stdio.h>
int main()
{


    HANDLE mutex;
    mutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"memtest");
    if(mutex == NULL)
    {
        mutex = CreateMutex(NULL,FALSE,"memtest");
    }
    printf("0X%X\n",mutex);
    printf("0X%X\n",WaitForSingleObject(mutex,INFINITE));//測試返回碼
    int x;
    while(scanf("%d",&x)!=EOF)
    {
        printf("x= %d\n",x);
        if(x == 10)
             break;
    }
    //ReleaseMutex(mutex);
    return 0;
}</span>