vendredi 28 octobre 2016

Is thread-safe comparison operator of two std::thread::id?

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:

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;:

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