How do I use std :: this_thread :: yield () deterministically?
When developing a VoIP based application, we have a generic C ++ 11 multithreaded module. It works fine on iOS, macOS, but finds it difficult to schedule threads on Android.
Additional construction description (only if interesting)
I have multiple threads working with message queues.
- Master (writes data to sockets received in the SSL_Read queue)
- SSL_Read (reads data from SSL and updates in the queue)
- SSL_Write (writes data to SSL which is received directly from Socket streams)
- Thread per Socket (reads data from the socket and sends to the SSL_Write queue)
1-2 are connected and 3-4 are connected.
I noticed that during many calls, only 2 threads are actively running and the other 2 threads are not getting runtime. In this regard, there is 1 way of the vocal tract.
Problem
I questioned this as Android Linux issues and for that I have an unanswered post:
C ++ 11 Android Multithreading Issues where some threads are not scheduled properly .
Went through std :: this_thread :: yield () usage?
The goal is to give the same time for all threads. Tried the following options:
- I decided to use
std::thread::yield()
when the message queue is full over a certain limit; for example 10 messages from 1 thread. i tried ityield()
1 time and 100 times in a loop but no advantage. The same thread continues to run. - The same for the
sleep_for()
0 and 100 ms options . The same thread continues to run. - Tried changing the value
nice()
to -10, -20 for all threads but no luck.
How to use effectively std::this_thread::yield()
without burning out too many CPU cycles?
source to share
To std::this_thread::yield()
legally do absolutely nothing. This allows the scheduler to schedule another thread; the scheduler shouldn't use it.
You may try:
-
Each thread capable of handling any work in progress, packing work into a common task queue. Thus, it doesn't matter which thread is doing the work, the most important work is done when there is a thread to do it.
-
Adding synchronization between threads such as a barrier mechanism (see for example a barrier raising class) to keep all 4 threads synchronized.
source to share