简体   繁体   中英

Lock-free thread pool

Is the following reasoning correct with respect to standard C++11 and/or also in a general context?

When implementing a thread pool one usually does not want to busy-loop those threads that have no work to do. Therefore, one needs some kind of std::condition_variable. Thus, one needs some kind of std::mutex. Thus, it is not possible to have a lock-free thread pool (that does not busy-loop).

Or am I missing some OS mechanism that would make it possible? Can you provide additional reasoning that suggests the non-existence of a lock-free non-busy-looping thread pool?

If you allow busy waiting, it is of course entirely possible. But without busy waiting, you need help from the OS - it doesn't have to be a mutex as such, it could of course be a read , poll , WaitForMultipleObject (in Windows) or other "wait for something" functionality provided by the OS.

I'm not 100% sure that it's IMPOSSIBLE to come up with something, but in general, a thread is either running, or it's held by the OS waiting for something. That "waiting for something in the OS" is, as far as I can see, always some sort of "lock".

However, the key here is this:

If locks in the case where there is "no work to do" is really a problem, then you probably should rethink your overall thread-pool usage, not try to remove the locks. Maybe larger packets of data needs to be put together by whatever "gives work to the threads".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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