Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 860
dzada Avatar asked Mar 24 '23 19:03

dzada


1 Answers

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.

like image 98
syam Avatar answered Apr 21 '23 00:04

syam