c++ - notify_one performance impact -
i reading bit std::condition_variable
, more particularly on how notify waiting thread using std::condition_variable::notify_one
.
i came across few questions happy answers on:
- what happens when thread calls
notify_one
(os-wise)? guess os-specific, sake of argument - i'm working in windows. - what happens if thread calls
notify_one
when there no waiting thread? call have performance impact (cpu-cycles, power etc)?
thanks
on windows, std::condition_variable
implemented in terms of native windows condition variables. see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682052(v=vs.85).aspx
on unix-like systems they're implemented in terms of pthreads semaphore/mutex pair.
the entire operation should take place in user space don't pay switch kernel mode, working 2 synchronisation primitives under covers. mean memory fences issued, there price pay.
to cut long story short, calling notify_one
when should, i.e. after changing state of condition , releasing lock, it's reasonably cheap operation. calling notify_one
in tight loop no reason not going idea.
what happens if thread calls notify_one when there no waiting thread?
take mutex, check whether there threads waiting, release mutex. end.
does call have performance impact (cpu-cycles, power etc)?
yes of course, consumes few cycles , requires cpu operating. doing once in while won't hurt. doing continuously in tight loop consume power.
i guess question is, "what's use case"? if you're adding million items second producer/consumer queue you're going spend lot of time , energy notifying nonexistent consumers. if you're adding 10 second, time spent in notify_one
won't show on performance trace.
Comments
Post a Comment