线程同步 信号量 Semaphore 内核对象 CreateSemaphore

来源:互联网 发布:中科大软件学院学费 编辑:程序博客网 时间:2024/05/01 14:04

0、思考

生产者消费者问题,同步互斥问题,有界缓冲器问题:1、两个进程对同一个内存资源操作,一个生产者,一个消费者;2、生产者往共享内存资源填充数据,如果区域满,则等待消费者消费数据;3、消费者从共享内存资源取数据,如果区域空,则等待生产者填充数据;4、生产者的填充数据行为和消费者的消费数据行为不可在同一时间发生。

1、相关api

CreateSemaphoreCreateSemaphoreExOpenSemaphoreReleaseSemaphoreCloseHandleWaitForSingleObjectWaitForMultipleObjects

2、写在前面

// 生产者消费者问题,同步互斥问题,有界缓冲器问题:// 互斥量mutex控制两者不能同时操作缓存区;信号量empty和full表示缓存区的空槽数量和满槽数量;// mutex初始为1,empty初始为n;full初始为0;// P、V(wait、signal)(down、up)(测试、增加)(-1、+1);// 生产者void Productor() {    while(1) {        P(&empty);        p(&mutex);        V(&mutex);        V(&full);    }}// 消费者void Consumer() {    while(1) {        P(&full);        P(&mutex);        V(&mutex);        V(&empty);    }}

3、api说明

// 创建信号量内核对象// lpSemaphoreAttributes:安全属性。(通常为NULL)// lInitialCount:资源中一开始有多少可供使用// lMaximumCount:应用程序能够处理的最大资源数// lpName:内核对象名称WINBASEAPI__out_optHANDLEWINAPICreateSemaphoreA(    __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,    __in     LONG lInitialCount,    __in     LONG lMaximumCount,    __in_opt LPCSTR lpName);WINBASEAPI__out_optHANDLEWINAPICreateSemaphoreW(    __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,    __in     LONG lInitialCount,    __in     LONG lMaximumCount,    __in_opt LPCWSTR lpName);#ifdef UNICODE#define CreateSemaphore  CreateSemaphoreW#else#define CreateSemaphore  CreateSemaphoreA#endif // !UNICODE// dwFlags:系统保留,设定为0。// dwDesiredAccess:访问权限。(通常为SEMAPHORE_ALL_ACCESS)WINBASEAPI__out_optHANDLEWINAPICreateSemaphoreExA(    __in_opt    LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,    __in        LONG lInitialCount,    __in        LONG lMaximumCount,    __in_opt    LPCSTR lpName,    __reserved  DWORD dwFlags,    __in        DWORD dwDesiredAccess);WINBASEAPI__out_optHANDLEWINAPICreateSemaphoreExW(    __in_opt    LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,    __in        LONG lInitialCount,    __in        LONG lMaximumCount,    __in_opt    LPCWSTR lpName,    __reserved  DWORD dwFlags,    __in        DWORD dwDesiredAccess);#ifdef UNICODE#define CreateSemaphoreEx  CreateSemaphoreExW#else#define CreateSemaphoreEx  CreateSemaphoreExA#endif // !UNICODE// 增加信号量的当前资源计数。// hSemaphore:指定内核对象// lReleaseCount:表示增加个数,必须大于0且不超过最大资源数量。// lpPreviousCount:当前资源计数,设为NULL表示不需要传出。(通常为NULL)(PS:没有办法在不改变当前资源计数的前提下得到当前值)WINBASEAPIBOOLWINAPIReleaseSemaphore(    __in      HANDLE hSemaphore,    __in      LONG lReleaseCount,    __out_opt LPLONG lpPreviousCount);// dwDesiredAccess:访问权限。(通常为SEMAPHORE_ALL_ACCESS)// bInheritHandle:信号量继承性。通常为true)// lpName:内核对象名称。WINBASEAPI__out_optHANDLEWINAPIOpenSemaphoreA(    __in DWORD dwDesiredAccess,    __in BOOL bInheritHandle,    __in LPCSTR lpName);WINBASEAPI__out_optHANDLEWINAPIOpenSemaphoreW(    __in DWORD dwDesiredAccess,    __in BOOL bInheritHandle,    __in LPCWSTR lpName);#ifdef UNICODE#define OpenSemaphore  OpenSemaphoreW#else#define OpenSemaphore  OpenSemaphoreA#endif // !UNICODE

4、C++封装

#pragma once#include <windows.h>class ncSemaphore{public:    ncSemaphore(LONG lInitialCount = 1, LONG lMaximumCount = 1, LPCTSTR lpName = NULL)    {        _sem = CreateSemaphore(NULL, lInitialCount, lMaximumCount, lpName);    }    ~ncSemaphore()    {        CloseHandle(_sem);    }public:    DWORD wait(DWORD timeout = INFINITE)    {        return WaitForSingleObject (_sem, timeout);    }    BOOL release (LONG lReleaseCount = 1, LPLONG lpPreviousCount = NULL)    {        return ReleaseSemaphore(_sem, lReleaseCount, lpPreviousCount);    }private:    HANDLE _sem;};

5、顺藤摸瓜

读者写者问题,同步互斥问题哲学家进餐问题,同步互斥问题放水果问题读文件问题阅览室问题单行道问题理发师问题

鸣谢

觉的我写的帮帮哒, 发个红包赏赏赏

1 0