[C++ Multithreading Series] [7] Implementing the Classic C/S Architecture

Requirements are as follows:

1. After the S-side receives the C-side request, it adds the C-side request to the priority queue. The S-side has a dedicated thread A to process the request in the queue. The thread is not blocked, and thread A gets the request from the queue. After that, a new thread will be created to execute the request, and the thread on the C side will block until the execution thread returns the result.

 

Based on this, the overall flow chart is as follows:

 

Key points of the problem:

1. The request thread needs to be blocked until the processing result is obtained.

2. The thread processing the queue cannot be blocked and needs to process the requests in the queue all the time.

3. After the execution thread is created, it needs to process the request independently and return the result to the requesting thread.

 

So there are two ways:

Method 1: Use condition variables to communicate between threads. Note two points: the request thread and the execution thread need to be on the same condition variable; they need to be locked on the same mutex.

Method 2: Use the atomic variable Done to communicate between two threads, and the request thread uses a while loop to block;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325021433&siteId=291194637