[libdispatch-dev] Possible race in _dispatch_logv when DISPATCH_DEBUG is defined?

DrPizza DrPizza at quiscalusmexicanus.org
Mon Jul 4 18:46:41 PDT 2011

The intended pattern in _dispatch_logv is to have a static FILE* logfile for the log file, lazily initialized by the first thread to call the function. To avoid locking, every thread that sees that logfile is empty opens the log file in a private per-thread FILE*, and then tries to cmpxchg that into the static value. Any thread that loses the race just closes its private copy and uses the shared one.

However, tmp has been marked static too, which means that if several threads are executing _dispatch_logv simultaneously, they'll clobber each other's tmp. That seems of dubious value to me; I think tmp should be non-static.


More information about the libdispatch-dev mailing list