window 信号量不同步例子

    xiaoxiao2022-06-28  26

    // testSemaphore.cpp : 定义控制台应用程序的入口点。 //参考:http://blog.csdn.net/wangweitingaabbcc/article/details/6833265 #include "stdafx.h" #include<windows.h> // 信号量对象句柄   HANDLE hSemaphore;  DWORD WINAPI RunThread(LPVOID lpParameter) { int CardNo = *(int*)(lpParameter); while ( 1 )

    {

    //INFINITE:永远等待 , 0:不等待, 数值就是多些ms。 

    WaitForSingleObject(hSemaphore, INFINITE);   //等待信号量 printf("aaaaaaaaaaa-%d \n",CardNo); ReleaseSemaphore(hSemaphore, 1, NULL);   Sleep(1000); } return 0; } int _tmain(int argc, _TCHAR* argv[]) { int nHandle = 1; hSemaphore = CreateSemaphore(NULL, 0, 1, NULL);   unsigned long dwThreadID; ::CreateThread( NULL, 0, &RunThread, &nHandle, 0, &dwThreadID ); Sleep(1000); ReleaseSemaphore(hSemaphore, 1, NULL);   getchar(); return 0;

    }

     

    解释:

    HANDLE CreateSemaphore(    LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // 安全属性指针    LONG lInitialCount, // 初始计数    LONG lMaximumCount, // 最大计数    LPCTSTR lpName // 对象名指针   );  //创建信号量

    BOOL ReleaseSemaphore(    HANDLE hSemaphore, // 信号量句柄    LONG lReleaseCount, // 计数递增数量    LPLONG lpPreviousCount // 先前计数   );  //释放信号量

     

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-1124271.html

    最新回复(0)