Overhead of calling pthread_cond_signal(), pthread_cond_broadcast(), std::condition_variable::notify_one() or std::condition_variable::notify_all()

During developing a tiny performance-critical part of a program I wondered if there were any significant overhead in calling "notify" on a condition variable if no-one were waiting on it. If someone is waiting on it it it will obviously go into the kernel and wake up that thread and have non-neglible overhead. But what if no-one is waiting? Is that cheap?

And is there a penalty for using the C++11 wrappers around pthread in this particular scenario?

Test program(s)

I wrote 4 small test programs. One for each of std::condition_variable::notify_one(), std::condition_variable::notify_all(), pthread_cond_signal() and pthread_cond_broadcast(). The program creates a brief thread to excercise the condition variable to ensure that any lazy resource allocation is not skewing the results. And then it calls the interesting function a million times.

Test platform

CPU
Intel Core i7-4770K @ 3.50GHz
Kernel
4.12.14-lp150.12.28-default
glibc
glibc-2.26-lp150.11.9.1.x86_64

Results
Function Time Nanoseconds per call
std::condition_variable::notify_one() 0.004 seconds 3.884962
std::condition_variable::notify_all() 0.004 seconds 3.600878
pthread_cond_signal() 0.002 seconds 2.351499
pthread_cond_broadcast() 0.003 seconds 2.628825

Conclusions

  1. The overhead of calling a wakeup function if no-one is waiting is neglible
  2. The C++11 wrappers have some overhead but I wouldn't worry about it

Test programs