Multithreading the fourth second Kill a classic multithreading synchronization problem

Source: Internet
Author: User

former " second-kill multithreading the third atom Operation interlocked series functions describes the role of atomic operations in multiple processes, and now comes to a complex point. This problem involves thread synchronization and mutual exclusion, is a very representative multi-threaded synchronization problem, assuming that the problem can be clear, then the multi-threaded synchronization has laid a good foundation.

Descriptive description of the program:

The main thread starts a child thread and passes the variable address representing the subroutine number to the child thread as a parameter.

A child thread receives a number of parameters, such as sleep ( 0) , global variable + +, and global variables.

Requirements:

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

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 child thread and passes in a pointer to the address of the variable, because it takes a certain amount of time for the thread to start, so the child thread can access and save the data based on the pointer. The mainline thread executes waits for the child thread to be saved and then modifies the parameter and starts the next thread.

This involves synchronization between the main thread and the child thread .

2. Modify and output global variables that are mutually exclusive between child threads. Requires that the output of global variables be incremented.

This involves mutual repulsion among the sub-threads .

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

Classic thread synchronization Mutual exclusion problem # include <stdio.h> #include <process.h> #include <windows.h>long g_nnum; global resource unsigned int __stdcall fun (void *ppm); thread function const int thread_num = 10; Number of child threads int main () {g_nnum = 0; HANDLE  Handle[thread_num];int i = 0;while (i < thread_num) {Handle[i] = (HANDLE) _beginthreadex (NULL, 0, fun, &i , 0, NULL), i++;//the main thread may have changed the value of this I when it received a parameter}//ensure that the child thread has all execution ends WaitForMultipleObjects (thread_num, handle, TRUE, INFINITE);  return 0;} unsigned int __stdcall fun (void *ppm) {//Because creating a thread is a certain overhead, so the new thread does not run to this for the first time int nthreadnum = * (int *) PPM;//Sub-thread gets the number of parameters sleep (50 );//some work should to dog_nnum++;  Handle Global resource sleep (0);//some work should to doprintf ("Thread number%d  global resource value is%d\n", Nthreadnum, G_nnum); return 0;}

The results can be taken to the following illustrations, and it is strongly recommended that the reader try it out for themselves.

Figure 1

Figure 2

Figure 3

It can be seen that the result of execution is completely chaotic and unpredictable.

This series will use various tools under the Windows platform to include critical segments, events, and mutually exclusive amounts. Signal volume and so on to solve the problem and make a comprehensive summary, please pay attention.

"Second-kill multi-threaded fifth Classic thread synchronization key section CS" has been announced, welcome to participate.

" second-kill multithreading the sixth classic thread synchronization events event" has been announced, welcome to visit.

" second-kill multi-threaded seventh" classic thread synchronization mutual exclusion amount of Mutex has been published, welcome to participate.

" second-kill multithreaded eighth" classic thread synchronization semaphore Semaphore" has been published. You are welcome to participate.

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

Multithreading the fourth second Kill a 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.