Use Win32API to synchronize producer and consumer threads

Source: Internet
Author: User

Use Win32API to synchronize producer and consumer threads

Use win32 API to create a thread and create a semaphore for Thread Synchronization

Create semaphores

Syntax:

 

HANDLE semophore;semophore =  CreateSemaphore(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName);

 

The following is a prototype of the CreateSemophore function:

 

Handle winapi CreateSemaphore (_ In_opt _ LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // attribute _ In _ LONG lInitialCount, // Initial Value _ In _ LONG lMaximumCount, // maximum value _ In_opt _ LPCTSTR lpName // The Name Of The semaphore object );
The function returns a HANDLE pointing to the semaphore object, which is a HANDLE object.

Wait and signal operations

The usage structure is generally as follows:

 

While (true) {WaitForSingleObject (semophore, INFINITE); // wait operation // ReleaseSemaphore (semophore, 1, NULL) in the critical section; // signal operation}

 

The prototype of the two functions is as follows:

 

DWORD WaitForSingleObject (HANDLE hHandle, // HANDLE, which can point to the semaphore or thread DWORD dwMilliseconds // Number of milliseconds to wait); BOOL ReleaseSemaphore (HANDLE hSemaphore, // HANDLE, pointing to the semaphore LONG lReleaseCount, // add value to the semaphore, which is generally 1 LPLONG lpPreviousCount // the pointer to the variable that saves the value before the semaphore );

Create thread

 

HANDLE producer; // The PRODUCER thread executes this function // pro_id thread idproducer = CreateThread (NULL, 0, PRODUCER, NULL, 0, & pro_id); // create a thread

 

Function prototype

 

Handle winapi CreateThread (_ In_opt _ LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread attribute struct _ In _ SIZE_T dwStackSize, // The initialization size of the stack, 0 indicates the default size of _ In _ LPTHREAD_START_ROUTINE lpStartAddress, // The starting address, that is, the pointer of the thread function _ In_opt _ LPVOID lpParameter, // The pointer of the thread function parameter _ In _ DWORD dwCreationFlags, // controls the flag bit to be created. 0 indicates that the thread is executed immediately after creation, and create_suincluded indicates that the thread is created In the suincluded state, it is not executed until the ResumeThread is called. The STACK_SIZE_PARAM_IS_A_RESERVATION parameter specifies the size of the initial reserved stack. If this parameter is not specified, dwStackSize specifies the submitted size _ Out_opt _ LPDWORD lpThreadId // pointer to the thread id );


 

The producer consumer thread synchronization code is as follows:

# Include
 
  
# Include
  
   
# Define QUEUE_LENGTH 10 // buffer length HANDLE full_sem; // full semaphores HANDLE empty_sem; // empty semaphores struct Msg {int I ;}; // message structure Msg MsgQueue [QUEUE_LENGTH]; // buffer int head = 0; // queue header int tail = 0; // queue tail dword winapi consumer (LPVOID lpParameter) {// consumer thread for (int I = 0; I ++) {WaitForSingleObject (full_sem, INFINITE); // wait operation // WaitForSingleObject (mutex, INFINITE ); // multiple consumers need to add mutex semaphores int val = MsgQueue [head]. i; head = (head + 1) % QUEUE_LENGTH; // ReleaseSemaphore (mutex, 1, NULL); // signal operation std: cout <"CONSUMER:" <
   
    

 

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.