Is thread-safe comparison operator== of std::thread::id
and std::this_thread::get_id()
as below?
std::thread::id t_id;
if(t_id == std::this_thread::get_id()) {}
For example, Boost 1.62 uses to compare threads id:
-
pthread_equal(owner,pthread_self())
for Linux: http://ift.tt/2dOQCPC -
::boost::detail::interlocked_read_acquire(&locking_thread_id)==current_thread_id
for Windows: http://ift.tt/2eiKaN4
As known pthread_equal()
is thread-safe and interlocked_read_acquire()
is thread safe.
But how can I compare two std::thread::id
without std::mutex
in C++11/14?
Can I use for this as below?
std::atomic<std::thread::id> t_id;
if(t_id.load(std::memory_order_acquire) != std::this_thread::get_id())
Result for std::atomic<std::thread::id> t_id;
:
- Linux since version GCC 4.7.3: http://ift.tt/2eiKSd8
- Linux since version Clang 3.3: http://ift.tt/2dORu6B
Result for both:
t_id.is_lock_free() = true
sizeof(t_id) = 8
- Windows MSVS2013 (v120):
Result:
t_id.is_lock_free() = true
sizeof(t_id) = 8
- Windows MSVS2013 (v120):
Result:
t_id.is_lock_free() = true
sizeof(t_id) = 4
Does is_lock_free() = true
mean, that (t_id.load(std::memory_order_acquire) != std::this_thread::get_id())
is thread safe and does not use std::mutex
?
Aucun commentaire:
Enregistrer un commentaire