One-day Windows API training (47) CreateSemaphore and ReleaseSemaphore Functions

Source: Internet
Author: User

During software development, multi-threaded programs often need to communicate with each other. For example, when several threads Add a message to the queue, while the other thread is sleeping, you need to wake up the thread to handle the problem. In this case, the semaphore must be used for synchronization. CreateSemaphore is the creation of semaphores, and ReleaseSemaphore is the addition of semaphores.
 
The CreateSemaphore and ReleaseSemaphore functions are declared as follows:
WINBASEAPI
_ Out
HANDLE
WINAPI
CreateSemaphoreA (
_ In_opt LPSECURITY_ATTRIBUTES l1_maphoreattributes,
_ In LONG lInitialCount,
_ In LONG lMaximumCount,
_ In_opt LPCSTR lpName
);
WINBASEAPI
_ Out
HANDLE
WINAPI
CreateSemaphoreW (
_ In_opt LPSECURITY_ATTRIBUTES l1_maphoreattributes,
_ In LONG lInitialCount,
_ In LONG lMaximumCount,
_ In_opt LPCWSTR lpName
);
# Ifdef UNICODE
# Define CreateSemaphore CreateSemaphoreW
# Else
# Define CreateSemaphore CreateSemaphoreA
# Endif //! UNICODE
 
Lsf-maphoreattributes is the secure attribute of the semaphore.
LInitialCount is the initial semaphore.
LMaximumCount allows the semaphore to be increased to the maximum value.
LpName is the name of the semaphore.
 
WINAPI
ReleaseSemaphore (
_ In HANDLE hSemaphore,
_ In LONG lReleaseCount,
_ Out_opt LPLONG lpPreviousCount
);
 
HSemaphore is the semaphore handle to be added.
LReleaseCount is the increase count.
LpPreviousCount is the value returned before the increase.
 
An example of calling a function is as follows:
#001 // run the function in a thread.
#002 // here, you can use the members in the class or make the derived class more powerful.
#003 // Cai junsheng 2007/10/10 QQ: 9073204 Shenzhen
#004 DWORD CThreadSemaphore: Run (void)
#005 {
#006 // output to the debugging window.
#007: OutputDebugString (_ T ("Run () thread function Run \ r \ n "));
#008
#009 //
#010 const LONG cMax = 10;
#011 m_hSemaphore = CreateSemaphore (
#012 NULL, // default security attribute.
#013 0, // The Initialization is 0 semaphores.
#014 cMax, // a maximum of 10 semaphores.
#015 NULL); // It is not named.
#016
#017 if (m_hSemaphore = NULL)
#018 {
#019 return-1;
#020}
#021
#022 //
#023 const int nMaxObjs = 2;
#024 HANDLE hWaitObjects [nMaxObjs] = {m_hEventExit, m_hSemaphore };
#025
#026 // thread loop.
#027 (;;)
#028 {
#029 DWORD dwRet = WaitForMultipleObjects (nMaxObjs, hWaitObjects, FALSE, INFINITE );
#030 if (dwRet = WAIT_TIMEOUT)
#031 {
#032 // you can continue running.
#033 TCHAR chTemp [1, 128];
#034 wsprintf (chTemp, _ T ("CThreadSemaphore: Run () ThreadID = % d \ r \ n"), m_dwThreadID );
#035: OutputDebugString (chTemp );
#036
#037 // if nothing is done at present, let the thread release the CPU.
#038 Sleep (10 );
#039}
#040 else if (dwRet = WAIT_OBJECT_0)
#041 {
#042 // exit the thread.
#043: OutputDebugString (_ T ("Run () Exit thread \ r \ n "));
#044 break;
#045}
#046 else if (dwRet = WAIT_OBJECT_0 + 1)
#047 {
#048 // you can continue running.
#049 TCHAR chTemp [1, 128];
#050 wsprintf (chTemp, _ T ("CThreadSemaphore: Run () Semaphore, ThreadID = % d \ r \ n"), m_dwThreadID );
#051: OutputDebugString (chTemp );
#052
#053 //
#054
#055}
#056 else if (dwRet = WAIT_ABANDONED)
#057 {
#058 // error.
#059: OutputDebugString (_ T ("Run () thread error \ r \ n "));
#060 return-1;
#061}
#062}
#063
#064 //
#065 if (m_hSemaphore)
#066 {
#067 CloseHandle (m_hSemaphore );
#068 m_hSemaphore = NULL;
#069}
#070
#071 return 0;
#072}
#073
The second line is to create a semaphore.
Semaphore events and exit events such as line 1.
 
 
#001
#002 //
#003 // Add a semaphore
#004 // Cai junsheng 2007/10/10 QQ: 9073204 Shenzhen
#005 //
#006 void IncSemaphore (void)
#007 {
#008 if (m_hSemaphore)
#009 {
#010 if (! ReleaseSemaphore (
#011 m_hSemaphore, // The semaphore to be added.
#012 1, // Add 1.
#013 NULL) // you do not want to return the previous semaphore.
#014 {
#015
#016}
#017}
#018}
#019

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/caimouse/archive/2007/10/10/1819116.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.