简体   繁体   English

在不使用条件变量的情况下唤醒线程的最快方法

[英]fastest way to wake up a thread without using condition variable

I am trying to speed up a piece of code by having background threads already setup to solve one specific task. 我试图通过设置后台线程来解决一个特定任务来加速一段代码。 When it is time to solve my task I would like to wake up these threads, do the job and block them again waiting for the next task. 当我需要解决我的任务时,我想唤醒这些线程,完成工作并再次阻止它们等待下一个任务。 The task is always the same. 任务总是一样的。

I tried using condition variables (and mutex that need to go with them), but I ended up slowing my code down instead of speeding it up; 我尝试使用条件变量(以及需要使用它们的互斥锁),但我最终减慢了代码速度,而不是加快速度; mostly it happened because the calls to all needed functions are very expensive ( pthread_cond_wait/pthread_cond_signal/pthread_mutex_lock/pthread_mutex_unlock ). 主要是因为对所有需要的函数的调用非常昂贵( pthread_cond_wait/pthread_cond_signal/pthread_mutex_lock/pthread_mutex_unlock )。

There is no point in using a thread pool (that I don't have either) because it is a too generic construct; 使用线程池没有任何意义(我没有),因为它是一个过于通用的构造; here I want to address only my specific task. 在这里,我只想解决我的具体任务。 Depending on the implementation I would also pay a performance penalty for the queue. 根据实现情况,我还会为队列支付性能损失。

Do you have any suggestion for a quick wake-up without using mutex or con_var ? 你有什么建议可以在不使用mutexcon_var情况下快速唤醒吗?

I was thinking in setup threads like timers reading an atomic variable ; 我在设置线程中思考像定时器读取atomic variable ; if the variable is set to 1 the threads will do the job; 如果变量设置为1,则线程将完成工作; if it is set to 0 they will go to sleep for few microseconds (I would start with microsecond sleep since I would like to avoid using spinlocks that might be too expensive for the CPU). 如果它被设置为0,它们将进入睡眠状态几微秒(我将从微秒睡眠开始,因为我想避免使用可能对CPU来说太昂贵的spinlocks )。 What do you think about it? 你怎么看待这件事? Any suggestion is very appreciated. 任何建议都非常感谢。

I am using Linux, gcc, C and C++. 我使用的是Linux,gcc,C和C ++。

These functions should be fast. 这些功能应该很快。 If they are taking a large fraction of your time, it is quite possible that you are trying to switch threads too often. 如果它们占用了大部分时间,那么很可能是您经常尝试切换线程。

Try buffering up a work queue, and send the signal once a significant amount of work has accumulated. 尝试缓冲工作队列,并在累积大量工作后发送信号。

If this is impossible due to dependencies between the tasks, then your application is not amenable to multithreading at all. 如果由于任务之间的依赖性而无法做到这一点,那么您的应用程序根本不适合多线程。

In order to gain performance in a multithreaded application, spawn as many threads as there are CPUs, not a separate thread for each task. 为了在多线程应用程序中获得性能,产生与CPU一样多的线程,而不是每个任务的单独线程。 Otherwise you end up with a lot of overhead from context switching. 否则,您最终会从上下文切换中获得大量开销。

You may also consider making your algorithm more linear (ie by using non-blocking calls). 您还可以考虑使您的算法更加线性(即使用非阻塞调用)。

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

相关问题 如何使用条件变量唤醒多个线程 - How to wake up multiple threads using condition variable 如果存在条件/谓词,通知条件变量是否保证唤醒具有成功条件/谓词的线程? - Does notifying a condition variable guarantee the wake-up of a thread with a successful condition/predicate if one exists? 为什么 boost::condition_variable 可以使用 pthread_cond_signal 只唤醒一个线程 - Why boost::condition_variable can use pthread_cond_signal to wake up only one thread 如果没有线程需要唤醒,是否需要获取锁并通知condition_variable? - Is it necessary to acquire the lock and notify condition_variable if no thread needs to wake up? 线程何时从condition.wait()中唤醒 - when does a thread wake up from condition.wait() 线程调度程序仿真:唤醒和休眠Pthread的正确方法 - Thread Scheduler Simulation: Correct Way to Wake Up and Sleep a Pthread 在条件上更新变量的最快方法是什么? - What is the fastest way to update a variable on a condition? Spurios唤醒和条件变量 - Spurios wake up and condition variables 唤醒线程非常耗时 - Wake up of the thread is time consuming 在1个线程上运行的boost :: asio :: io_service如何唤醒等待状态 - How could a boost::asio::io_service running on 1 thread wake up awaiting condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM