The first 15 introduces the related concepts of multithreading, multi-threaded synchronization mutual Exclusion (fourth) and the common methods of solving multi-threaded synchronous mutual exclusion--key segment, event, mutex, semaphore, read-write lock. In order to make people more proficient in the use of multi-threading, there will be 10 articles to explain 10 multi-threaded use cases, I believe after reading these 10 will allow you to more comfortable use of multithreading.
First, the first--the 16th one of the top ten classic cases of multithreading dual-thread read-write queue data
One of the top ten classic cases of multithreading double-threaded read-write queue data case Description:
The response function for a button in the MFC dialog box implements two functions:
The display data simultaneously processes the data, therefore opens two threads, one thread displays the data (opens a timer, responds to the WM_TIMER message to add the data to the Teechart chart at a certain time interval and displays) simultaneously adds the data in the queue troop tail, another thread from the team queues the head to go to the data to process.
Here's how to solve this case. Let's start with the analysis.
One of the top ten classic cases of multithreading double-threaded read-write queue data case study:
This is a case where a thread reads data to the queue header in the queue, and a thread writes data to the queue's end. It looks much like the reader's question (see 11 and 14), but in fact, if the queue is considered a buffer, this case is obviously a producer consumer problem (see tenth). So we follow the idea of producer consumers to analyze the case of "wait" in detail:
1. When the queue is empty, the read data thread must wait for the write data to write to the queue. This means that when the queue is empty, the read data thread waits for the data in the queue .
2. When the queue is full, the write data thread must wait for the read data thread to read the data to the queue. that is, when the queue is full, write to the data thread to wait for the queue to have empty space.
Do I need to be mutually exclusive when accessing the queue? this will depend on the data structure implementation of the queue, if vectors in the STL are used, because the vectors will grow dynamically. Therefore, to do mutex protection. If a loop queue is used, the read data thread has a read pointer, the write data thread has a write pointer, each accesses data at a different location in the queue, so there is no mutex protection.
Once the analysis is complete, consider what data structure to use, and follow the same approach in the tenth article. Use two semaphores, one to record the number of slots in the loop queue, one to record the number of products in the loop queue (not the number of seats). The code is very easy to write, and the complete source code is given below.
The semaphore correlation function in the code can refer to the eighth chapter, the Setconsolecolor in the code is used to change the text color of the console, in particular, the VC console color settings can be consulted.
One of the top ten classic cases of multithreading double-threaded read-write queue data complete code:
[CPP]View PlainCopy
- 16th one of multithreading ten classic cases dual-thread read-write queue data
- http://blog.csdn.net/MoreWindows/article/details/8646902
- #include <stdio.h>
- #include <process.h>
- #include <windows.h>
- #include <time.h>
- const INT queue_len = 5;
- int G_arrdataqueue[queue_len];
- int g_i, G_j, G_ndatanum;
- Key sections are used to ensure mutually exclusive output on the screen
- Critical_section G_cs;
- The semaphore G_hempty indicates that the vacancy g_hfull in the queue indicates a non-vacancy in the queue
- HANDLE G_hempty, G_hfull;
- Setting the console output color
- BOOL Setconsolecolor (WORD wattributes)
- {
- HANDLE hconsole = GetStdHandle (Std_output_handle);
- if (hconsole = = Invalid_handle_value)
- return FALSE;
- return Setconsoletextattribute (Hconsole, wattributes);
- }
- Read Data thread functions
- unsigned int __stdcall readerthreadfun (PVOID pM)
- {
- int ndata = 0;
- While (Ndata <)
- {
- WaitForSingleObject (G_hfull, INFINITE);
- Ndata = G_arrdataqueue[g_i];
- G_i = (g_i + 1)% Queue_len;
- EnterCriticalSection (&g_cs);
- printf ("read data from the queue%d\n", ndata);
- LeaveCriticalSection (&g_cs);
- Sleep (rand ()% 300);
- ReleaseSemaphore (G_hempty, 1, NULL);
- }
- return 0;
- }
- Write Data thread functions
- unsigned int __stdcall writerthreadfun (PVOID pM)
- {
- int ndata = 0;
- While (Ndata <)
- {
- WaitForSingleObject (G_hempty, INFINITE);
- G_arrdataqueue[g_j] = ++ndata;
- G_j = (g_j + 1)% Queue_len;
- EnterCriticalSection (&g_cs);
- Setconsolecolor (Foreground_green);
- printf ("Write data%d to queue \ n", ndata);
- Setconsolecolor (foreground_red | Foreground_green | Foreground_blue);
- LeaveCriticalSection (&g_cs);
- Sleep (rand ()% 300);
- ReleaseSemaphore (G_hfull, 1, NULL);
- }
- return 0;
- }
- int main ()
- {
- printf ("16th multithreading ten classic cases double thread read and write queue data \ n");
- printf ("-by Morewindows (http://blog.csdn.net/MoreWindows/article/details/8646902)-\n\n");
- InitializeCriticalSection (&g_cs);
- G_hempty = CreateSemaphore (null, Queue_len, queue_len, NULL);
- G_hfull = CreateSemaphore (null, 0, queue_len, NULL);
- Srand (Time (NULL));
- g_i = G_j = 0;
- HANDLE hthread[2];
- Hthread[0] = (HANDLE) _beginthreadex (null, 0, readerthreadfun, NULL, 0, NULL);
- HTHREAD[1] = (HANDLE) _beginthreadex (null, 0, writerthreadfun, NULL, 0, NULL);
- WaitForMultipleObjects (2, Hthread, TRUE, INFINITE);
- For (int i = 0; i < 2; i++)
- CloseHandle (Hthread[i]);
- CloseHandle (G_hempty);
- CloseHandle (G_hfull);
- DeleteCriticalSection (&g_cs);
- return 0;
- }
One of the top ten classic cases of multithreading dual-thread read-write queue data run Result:
The results of the program run as follows:
This package is: http://download.csdn.net/detail/morewindows/5136035
Multi-threaded Face Question series (16): Multithreading one of the top ten classic cases dual-thread read and write queue data