dzada
dzada

Reputation: 5864

Is thread::get_id (C++11) lock free?

I would like to test the thread calling different functions of one of my classes. I have a critical time thread, and I don't want anyone to call a function that may call new to be called from that thread. However, as the 2 functions are public, I can't enforce it by the language.

My idea is to test the thread id. Assuming that I can ensure that the call initializing the thread id is in the right thread, I would just have to call thread::get_id() in other calls and compare to the thread id I saved.

The problem is that I also want to test in the critical thread this ID but I can not lock in that thread.

Therefore my question is: Is thread::get_id() lock free (and what could be the worst time of execution)?

Upvotes: 4

Views: 488

Answers (1)

syam
syam

Reputation: 15069

The Standard gives no guarantee either way as to whether thread::get_id() and std::this_thread::get_id() are lock-free or not, or even concerning their complexity.

I'm afraid the answer to your question is implementation-specific, depending on your particular Standard library and underlying threads library.

Upvotes: 7

Related Questions