C++20 is introducing https://en.cppreference.com/w/cpp/atomic/atomic/wait and https://en.cppreference.com/w/cpp/atomic/atomic/notify_one , which introduces atomic waiting and notification functionality.
I'd like to use this but my compiler doesn't support it yet.
Are there any reliable library implementations of this functionality?
You can use Boost.Atomic if your compiler's standard library implementation does not support it yet.
Am I correct that such wait/notify functionality would allow me to do what I want to do (ie, let my consumer thread go to sleep and let the producer efficiently wake it up without acquiring a mutex (something that would be necessary, I believe, with a condition variable).
Correct.
Actually, is there an even better way to do this that I'm not currently thinking of?
Atomic wait is a good way. You may also want to use it the best way:
futex
, and it takes 32-bit integer. On Windows, WaitOnAddress
can take any size of 8, 16, 32, or 64. On other systems consider their implementation of atomic wait.As other good way you can consider C++20 semaphores.
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.