Second-kill multithreading eighth classic thread sync semaphore semaphore

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Before reading this article, we recommend reading the following sister articles:

"Second-kill multithreaded fourth" a classic multi-threaded synchronization problem

"Second-kill multithreaded fifth" Classic thread synchronization key section CS

"Second-kill multithreaded sixth" Classic thread synchronization events event

"Second-kill multithreaded seventh" classic thread synchronization mutex mutex

The use of Critical segment CS, event events, mutex mutex in classic thread synchronization problems is described earlier. This article introduces the use of semaphore semaphore to solve this problem.

First also to see how to use the semaphore, Semaphore semaphore commonly have three functions, easy to use. Here are the prototypes and instructions for using these functions.

The first of the CreateSemaphore

function function: Create semaphore

Function Prototypes:

HANDLE CreateSemaphore (

Lpsecurity_attributes Lpsemaphoreattributes,

LONG lInitialCount,

LONG lMaximumCount,

LPCTSTR lpname

);

Function Description:

The first parameter represents the security control, which is typically passed directly to null.

The second parameter represents the initial resource quantity.

The third parameter represents the maximum number of concurrent quantities.

The fourth parameter represents the name of the semaphore, and passing in NULL indicates the amount of anonymous semaphore.

A second OpenSemaphore

function function: Turn on semaphore

Function Prototypes:

HANDLE OpenSemaphore (

DWORD dwDesiredAccess,

BOOL bInheritHandle,

LPCTSTR lpname

);

Function Description:

The first parameter represents access rights to the generic incoming semaphore_all_access. Detailed explanations can be viewed in the MSDN documentation.

The second parameter represents the semaphore handle inheritance, which is generally passed to true.

The third parameter represents a name, and each thread in a different process can make sure that they access the same semaphore by name.

A third ReleaseSemaphore

function function: The current resource count for incrementing semaphores

Function Prototypes:

BOOL ReleaseSemaphore (

HANDLE Hsemaphore,

LONG lReleaseCount,

Lplong Lppreviouscount

);

Function Description:

The first parameter is the handle to the semaphore.

The second parameter indicates the number of increments, which must be greater than 0 and not exceed the maximum number of resources.

The third parameter can be used to pass out the previous resource count, and set to NULL to indicate that no outgoing is required.

Note: the current number of resources is greater than 0, indicating that the semaphore is triggered, equals 0 indicates that the resource has been exhausted so the semaphore is triggered at the end. when the wait function is called on the semaphore, the wait function checks the current resource count of the semaphore if it is greater than 0 (that is, the semaphore is in the trigger state), minus 1, and returns the calling thread to continue execution. A thread can call the wait function multiple times to reduce the semaphore.

Cleanup and destruction of the last signal volume

Because the semaphore is a kernel object, you can use CloseHandle () to complete the cleanup and destruction.

Set a semaphore and a key segment in the classic multithreaded problem. The synchronization of the main thread and the sub-thread is handled by the semaphore, and the mutual exclusion between the sub-threads is handled by the critical section. See the code:

[CPP]View PlainCopy
  1. #include <stdio.h>
  2. #include <process.h>
  3. #include <windows.h>
  4. Long G_nnum;
  5. unsigned int __stdcall fun (void *ppm);
  6. const int thread_num = 10;
  7. Signal volume and critical segment
  8. HANDLE G_hthreadparameter;
  9. Critical_section G_csthreadcode;
  10. int main ()
  11. {
  12. printf ("Classic thread sync semaphore semaphore\n");
  13. printf ("-by Morewindows (http://blog.csdn.net/MoreWindows)--\n\n");
  14. //Initialize semaphores and critical segments
  15. G_hthreadparameter = CreateSemaphore (null, 0, 1, NULL); //Current 0 resources, maximum allowed 1 simultaneous access
  16. InitializeCriticalSection (&g_csthreadcode);
  17. HANDLE Handle[thread_num];
  18. G_nnum = 0;
  19. int i = 0;
  20. While (i < thread_num)
  21. {
  22. Handle[i] = (handle) _beginthreadex (NULL, 0, fun, &i, 0, NULL);
  23. WaitForSingleObject (G_hthreadparameter, INFINITE); //wait for the semaphore >0
  24. ++i;
  25. }
  26. WaitForMultipleObjects (Thread_num, handle, TRUE, INFINITE);
  27. //Destroy the semaphore and key segment
  28. DeleteCriticalSection (&g_csthreadcode);
  29. CloseHandle (G_hthreadparameter);
  30. For (i = 0; i < thread_num; i++)
  31. CloseHandle (Handle[i]);
  32. return 0;
  33. }
  34. unsigned int __stdcall fun (void *ppm)
  35. {
  36. int nthreadnum = * (int *) PPM;
  37. ReleaseSemaphore (G_hthreadparameter, 1, NULL); //Signal volume + +
  38. Sleep (50); //some work should
  39. EnterCriticalSection (&g_csthreadcode);
  40. ++g_nnum;
  41. Sleep (0); //some work should
  42. printf ("Thread number%d global resource value is%d\n", Nthreadnum, G_nnum);
  43. LeaveCriticalSection (&g_csthreadcode);
  44. return 0;
  45. }

Running results such as:

As you can see, semaphores can also solve synchronization problems between threads.

Since the semaphore can calculate the current amount of the resource and determine whether the semaphore is in the trigger state or not triggered based on the current remaining amount compared with 0, the signal volume is widely used. This series of "Seconds to kill multi-threaded tenth" producer consumer issues will use it again to solve the thread synchronization problem, welcome to see.

At this point, the classic thread synchronization problem is all over, the next "Seconds to kill multithreading Nineth Classic multi-threaded synchronization Problem Summary" will be summed up to comb the knowledge points.

Reprint please indicate source, original address: http://blog.csdn.net/morewindows/article/details/7481609

If you feel that this article is helpful, please click on the ' top ' support, your support is my greatest motivation to write, thank you.

Second-kill multithreading eighth classic thread sync semaphore semaphore

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.