简体   繁体   English

如何确定性地使用std :: this_thread :: yield()?

[英]How to use std::this_thread::yield() deterministically?

While developing a VoIP based application, we have a common multi-threaded C++11 module. 在开发基于VoIP的应用程序时,我们有一个通用的多线程C ++ 11模块。 It works fine in iOS, MacOS but finds thread scheduling difficulty in Android. 它在iOS,MacOS中运行良好,但在Android中发现线程调度难度。

Optional Design Description (only if interested) 可选设计说明(仅在感兴趣时)

I have few threads running with message queues. 我有几个线程与消息队列一起运行。

  1. Master (writes data to sockets which is received in SSL_Read queue) Master(将数据写入SSL_Read队列中接收的套接字)
  2. SSL_Read (reads data from SSL and updates in its queue) SSL_Read(从SSL读取数据并在其队列中更新)
  3. SSL_Write (writes data to SSL which is received directly from Socket threads) SSL_Write(将数据写入直接从Socket线程接收的SSL)
  4. Thread per Socket (reads data from socket and send to SSL_Write queue) 每个套接字的线程数(从套接字读取数据并发送到SSL_Write队列)

1-2 are related and 3-4 are related. 1-2相关,3-4相关。
I have observed that during many calls, only 2 threads are actively running and other 2 threads don't get running time. 我观察到在许多调用期间,只有2个线程正在运行,而其他2个线程没有运行时间。 Due to which 1 way voice path is observed. 由于观察到哪种单向语音路径。

Problem 问题

I doubted this as an Android Linux issue and for that, I have an unanswered post already: 我怀疑这是一个Android Linux问题,为此,我有一个未答复的帖子:
c++11 multithreading issues with Android where some threads are not scheduled properly . c ++ 11 Android的多线程问题,其中一些线程没有正确安排
Went through std::this_thread::yield() usage? 经过std :: this_thread :: yield()用法?

The goal is to give similar time slicing to all threads. 目标是为所有线程提供类似的时间切片。 Tried following options: 尝试以下选项:

  1. I decided to use std::thread::yield() when the message queue is filled up beyond certain limit; 当消息队列填满超出一定限度时,我决定使用std::thread::yield() ; eg 10 messages from 1 thread. 例如来自1个线程的10条消息。 I tried yield() 1 time and 100 time in loop, but there is no advantage of it. 我在循环中尝试了yield() 1次和100次,但没有优势。 The same thread continue running. 同一个线程继续运行。
  2. Same thing for sleep_for() option with 0 and 100 ms. sleep_for()选项的内容与0和100毫秒相同。 Same thread keep running. 相同的线程继续运行。
  3. Tried changing nice() value to -10, -20 for all threads but no luck. 尝试将所有线程的nice()值更改为-10,-20,但没有运气。

How to use std::this_thread::yield() effectively without burning out too many CPU cycles? 如何有效地使用std::this_thread::yield()而不会烧掉太多的CPU周期?

It is legal for std::this_thread::yield() to do absolutely nothing. std::this_thread::yield()绝对没有任何作用是合法的。 It provides an opportunity for the scheduler to schedule another thread; 它为调度程序提供了安排另一个线程的机会; the scheduler doesn't have to take advantage of it. 调度程序不必利用它。

You could try either: 你可以试试:

  1. Having each thread able to process whatever work needs doing, by packaging the work into a generic task queue. 通过将工作打包到通用任务队列中,让每个线程都能够处理任何工作需要做的事情。 That way it doesn't matter which thread takes the work, the most important work gets done when there is a thread to do it. 这样,哪个线程完成工作并不重要,当有线程执行时,最重要的工作就完成了。

  2. Adding synchronization between the threads such as a barrier mechanism (see eg boost's barrier class) to keep all 4 threads in sync. 在线程之间添加同步,例如屏障机制(参见例如boost的屏障类),以保持所有4个线程同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM