Turn--Seconds to kill multithreading fourth one classic multithreading synchronization problem

Source: Internet
Author: User

The second multi-threaded third atomic operations interlocked series function describes the role of atomic operations in multiple processes, now a complex point. This problem involves thread synchronization and mutual exclusion, is a very representative multi-threaded synchronization problem, if the problem can be clear, then the multi-threaded synchronization has laid a good foundation.

Program Description:

The main thread initiates 10 child threads and passes the variable address representing the subroutine number as a parameter to the child thread. The child thread receives the parameter, sleep ( 0), the global variable + +, and the global variable.

Requirements:

1. The line program number that the child thread outputs cannot be duplicated.

2. The output of the global variable must be incremented.

Here's a simple one:

Analysis of the problem of the study point, the main points of study are two:

1. The main thread creates a sub-thread and passes in a pointer to the address of the variable as a parameter, because the thread starts to take a certain amount of time, so before the child thread accesses and saves the data according to the pointer, the main line thread executes waits for the child thread to save the parameter and starts the next thread. This involves synchronization between the main thread and the child thread .

2. There are mutually exclusive changes and output global variables between the child threads. Requires that the output of global variables be incremented. This involves mutual exclusion between sub-threads .

The basic framework of this program is listed below, which can be modified and validated based on this code.

[CPP]View Plaincopy
  1. Classic thread Synchronization Mutex problem
  2. #include <stdio.h>
  3. #include <process.h>
  4. #include <windows.h>
  5. Long G_nnum; //Global resource
  6. unsigned int __stdcall fun (void *ppm); //thread function
  7. const int thread_num = 10; //number of child threads
  8. int main ()
  9. {
  10. G_nnum = 0;
  11. HANDLE Handle[thread_num];
  12. int i = 0;
  13. While (i < thread_num)
  14. {
  15. Handle[i] = (handle) _beginthreadex (NULL, 0, fun, &i, 0, NULL);
  16. i++; //The main thread may have changed the value of this I when it received the parameter
  17. }
  18. //guarantee that the child threads are all running end
  19. WaitForMultipleObjects (Thread_num, handle, TRUE, INFINITE);
  20. return 0;
  21. }
  22. unsigned int __stdcall fun (void *ppm)
  23. {
  24. Because creating a thread is a certain overhead, the new thread is not able to do it the first time.
  25. int nthreadnum = * (int *) PPM; //Sub-thread get parameters
  26. Sleep (50); //some work should
  27. g_nnum++; //Processing global Resources
  28. Sleep (0); //some work should
  29. printf ("Thread number%d global resource value is%d\n", Nthreadnum, G_nnum);
  30. return 0;
  31. }

The results of the operation can refer to the following illustrations, and it is strongly recommended that you try them yourself.

Figure 1

Figure 2

Figure 3

As you can see, the running results are completely chaotic and unpredictable. This series will use the various tools under the Windows platform, including key segments, events, mutexes, semaphores and so on to solve this problem and make a comprehensive summary, please pay attention.

Turn--Seconds to kill multithreading fourth one classic multithreading synchronization problem

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.