Debug version should do better job of checking ownership

Debug version should do better job of checking ownership

This note suggests a change to improve debuggability.

Summary:In a calllike t1->spawn(t2), it is required that task t1 be owned by the current thread. If not, subtle races that corrupt the scheduler can result.Thus the requirement should be checked by an assertion in the debug version of the library. The rest of this note outlines how to add the assertion.

Analysis: Each task object has a pointer to a scheduler object. Seven of the operations on a task object t use the virtual dispatch sequence t.prefix().owner->foo().Ownership must be checked before the dispatch, otherwise the dispatch might be on a non-existent scheduler object.Some of these dispatches occur in the header task.h, so the check is needed in the header and the DLL. The check requires the result from GetThreadSpecific(), which is not exported from the DLL. GetThreadSpecific should not be exported because it is a low-level interface. So I propose creating a checking method and exporting it from the DLL. Heres the definition:

bool task::is_owned_by_current_thread() const {

return GetThreadSpecific()==prefix().owner;


It would be present in both the debug and release versions of the DLL for sake of consistent linkage.Then in the header, add assertions to do the checking. E.g., the blue below shows one place to insert it:

void spawn( task& child ) {

__TBB_ASSERT( is_owned_by_current_thread(),

"'this' not owned by current thread" );

prefix().owner->spawn( child, child.prefix().next );


1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.