C++11 -- multithreaded programming 6

Keywords: C++11

In this article, we will discuss the necessity of event processing in multithreading

Sometimes a thread needs to wait for something to happen, such as the condition is true or the task is completed through another thread

such as

Suppose we are building a web-based application. This application performs the following tasks:,

  1. Handshake with server
  2. Load data from an XML file.
  3. Process the data loaded from XML.

As we can see, Task 1 does not depend on any other task, while Task 3 depends on Task 2. Therefore, this means that Task 1 and task 2 can be run in parallel by different threads to improve the performance of the application.

So let's break it down into a multithreaded application,

Now, it includes two threads,

The responsibility of thread 1 is,

  • Perform some handshake with the server.
  • Wait for thread 2 to load data from XML
  • Process the data loaded from XML.

The responsibility of thread 2 is,

  • Load data from XML
  • Notify another thread to wait for a message.




Above, thread 1 performs some operations and then waits for the event / condition to occur. The event or condition here is,

Data loading succeeded.

Once thread 1 receives the event, it performs some processing on the data.

Thread 2, when thread 1 is busy holding the mobile phone, load data in parallel.

When thread 2 successfully loads data from XML, it then notifies thread 1 by signaling the event.

Now, when an event or condition signals, thread 1 will continue to process the data.

What are the benefits of making it multithreaded?

When thread 1 is busy with some handshake mechanism, thread 2 will load data from XML in parallel. Therefore, it will improve the performance of the application.

Implementation method:

Implementation mode

First kind
Create a boolean global variable with the default value of false, set it to true in thread 2, and thread 1 will cycle to detect its value. Once the value is set to true, thread 1 will continue to process data,
Since it is a global variable shared by two threads, it needs to be synchronized with mutex lock

class Application
 std::mutex m_mutex;
 bool m_bDataLoaded;
 m_bDataLoaded = false;
 void loadData()
 // Make This Thread sleep for 1 Second
 std::cout<<"Loading Data from XML"<<std::endl;
 // Lock The Data structure
 std::lock_guard<std::mutex> guard(m_mutex);
 // Set the flag to true, means data is loaded
 m_bDataLoaded = true;
 void mainTask()
 std::cout<<"Do Some Handshaking"<<std::endl;
 // Acquire the Lock
 // Check if flag is set to true or not
 while(m_bDataLoaded != true)
  // Release the lock
  //sleep for 100 milli seconds
  // Acquire the lock
  // Release the lock
  //Doc processing on loaded Data
  std::cout<<"Do Processing On loaded Data"<<std::endl;
int main()
  Application app;
  std::thread thread_1(&Application::mainTask, &app);
  std::thread thread_2(&Application::loadData, &app);
  return 0;



This method has the following defects:
In order to detect variables, the thread will continue to acquire release locks, which will consume CPU cycles and slow down thread 1 because it needs to acquire the same locks to update the bool variables.
Therefore, obviously, we need a better implementation mechanism. For example, in some way, thread 1 can block by waiting for the event signal, and another thread can notify the event and make thread 1 continue. This will have the same CPU cycles and better performance.

We can use conditional variables. Conditional variables are events used for signaling between two threads. One thread can wait for it to get a signal, and other threads can send a signal to it.
The next section details this condition variable and uses it to solve the problem.

Posted by padma on Mon, 08 Nov 2021 23:25:20 -0800