lundi 27 mai 2019

Does a thread synchronize with with the previous thread with same id?

In C++11 there is std::this_thread::get_id() I can use to obtain an std::thread::id identifier of my thread. The standard says:

30.3.1.1

  1. An object of type thread::id provides a unique identifier for each thread of execution and a single distinct value for all thread objects that do not represent a thread of execution (30.3.1). Each thread of execution has an associated thread::id object that is not equal to the thread::id object of any other thread of execution and that is not equal to the thread::id object of any std::thread object that does not represent threads of execution.
  2. thread::id shall be a trivially copyable class (Clause 9). The library may reuse the value of a thread::id of a terminated thread that can no longer be joined.

My question is precisely about the case in which a new thread B, has the same id as an old thread A: will the thread B "see changes made by" thread A?

To be more specific consider this scenario:

Thread A does:

owner.store(std::this_thread::get_id()); //store A's 'id'
...some work...
owner.store(std::thread::id(),std::memory_order_relaxed); // clear

Thread B does:

assert(owner.load(std::memory_order_relaxed) != std::this_thread::get_id());

Will this assert hold?

The owner.load in Thread A and last owner.store in Thread B are intentionally "relaxed", as so there is no obvious "synchronize with" relation between the two, other than the one supposed in my question.

Aucun commentaire:

Enregistrer un commentaire