|
|
|
|
|
by aw1621107
258 days ago
|
|
> Implementation of __gthread_active_p is indeed a runtime check [3] which AFAICS applies only to single-threaded programs. Perhaps the shared-library use-case also fits here? The line you linked is for some FreeBSD/Solaris versions which appear to have some quirks with the way pthreads functions are exposed in their libc. I think the "normal" implementation of __gthread_active_p is on line 248 [0], and that is a pretty straightforwards check against a weak symbol. > Strange optimization IMHO so I wonder what was the motivation behind it. I believe the motivation is to avoid needing to pay the cost of atomics when there is no parallelism going on. > The cost function being optimized in this case is depending on WORD being atomic [4] without actually using the atomics [5]. Not entirely sure what you're getting at here? The former is used for single-threaded programs so there's ostensibly no need for atomics, whereas the latter is used for non-single-threaded programs. [0]: https://codebrowser.dev/kde/include/x86_64-linux-gnu/c++/11/... |
|
> I believe the motivation is to avoid needing to pay the cost of atomics when there is no parallelism going on.
Obviously yes. What I am wondering is what benefit does it bring in practice. Single-threaded program with shared-ptr's using atomics vs shared-ptr's using WORDs seem like a non-problem to me - e.g. I doubt it has a measurable performance impact. Atomics are slowing down the program only when it comes to contention, and single-threaded programs can't have them.