RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
C++

Try condition_variable using C++Builder(C++11).

Author: h.mohri

std::condition_variable is a class for waiting for thread execution until the condition is satisfied.
Used in combination with std::mutex.

I tried the wait() function in std::condition_variable.
This is combined with std::unique_lock<std::mutex>.

I thought of the following specifications.
2017-08-1014551.png
As shown in the figure, create multiple threads.
All threads are in Wait state.
Then let the threads run one by one.

Make mutex and condition_variable management class.

The created thread is waited using condition_variable.
Sleeping for 1 second immediately after passing Wait.
Then execution request is made to the next thread.

turn_thread.h

Thread creation function

It is a button event of C++Builder.
We create 10 threads, but they are all waited.
Thereafter, to perform one single thread start_and_join() function.

Unit1.cpp

Execution result

Threads are executed in an irregular way one by one.

 

 

 

 

 


 

In C++Builder(C++11), use timed_mutex.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Deep Dive Webinar: Boost C++Builder Compile Speed with TwineCompile
Related posts
C++Delphi

Get Free Responsive Cross-Platform Login Screen Templates For Android And iOS

C++CodeDelphi

Learn To Build A Python GUI For Working With The Numpy Library In A Delphi Windows App

C++Code

Learn About Using Right Angle Brackets In This C++11 Feature For Windows Development

C++DelphiRAD Studio

Learn How To Use WinRT In Your Native Windows Delphi And C++ Builder Apps

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES