Library: ensure thread-ID is initialised at thread start
While testing, I repeatedly had SEGFAULT in the new thread-wrapper, but only when running under debugger. While the language spec guarantees that exit from the thread handle initialisation synchronizes-with the start of the new thread, there is no guarantee in the reverse direction. Here this means that the new thread may not see the newly initialised thread handle ID at start. Thus I've added a yield-wait at the very beginning of the new thread function. Under normal conditions, the startup of a thread takes at least 100 - 500µs and thus I've never seen the problematic behaviour without debugger. However, adding a yield-wait loop at that point seems harmless (it typically checks back every 400ns or so). All real usages of the thread wrapper in the application use some kind of additional coordination or even a sync barrier to ensure the thread can pick up all further data before going into active work. WARNING: if someone would detach() the thread immediately after creating it, then this added condition would cause the starting thread function to hang forever. In our current setup for the thread wrapper, this is not possible, since the thread handle is embedded into protected code. The earliest point you could do that would be in the handle_begin_thread(), which is called from the thread itself *after* the new check. And moreover, this would require to write a new variation of the Policy.
This commit is contained in:
parent
8056bebf9c
commit
3c3d31dd40
1 changed files with 2 additions and 0 deletions
|
|
@ -391,6 +391,8 @@ namespace lib {
|
|||
void
|
||||
invokeThreadFunction (ARGS&& ...args)
|
||||
{
|
||||
while (not Policy::isLive()) // wait for thread-ID to become visible
|
||||
std::this_thread::yield();// (typically happens when debugging)
|
||||
Policy::handle_begin_thread();
|
||||
Policy::markThreadStart();
|
||||
Policy::perform_thread_function (forward<ARGS> (args)...);
|
||||
|
|
|
|||
Loading…
Reference in a new issue