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)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With