Is it okay to use std::this_thread* functions from boost::threads?

Claudiu

Is it okay to mix and match things from boost::thread and std::thread, or should one set of functions be used for each?

I ask because my code uses boost::threads, but I've found that boost::this_thread::sleep_for doesn't behave properly when setting the system time back, but std::this_thread::sleep_for does, so I'd like to change my sleep function call and avoid changing all my boost::threads to std::threads if possible.

sehe

In practice you might get away with things iff/because the implementations use the same implementations (e.g. pthread on linux).

However, you will be breaking invariants. Simple example: Boost Thread's interruption points won't function with non-boost synchronisation primitives (including std::this_thread::sleep_*).

Therefore I'd avice against actually mixing libraries for controlling related threads, lest you want to risk running into suprises ¹

Of course, if libraries have completely separate concerns (e.g. they use threads internally, "in the black box"), there should be no issue combining those libraries in one process.


¹ I can see deadlocks happening, and data races/leaks do not require a huge stretch of imagination (think thread local data support/call_once/set_value_at_thread_exit...)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it okay to use many anonymous functions?

Where is std::this_thread for jthread?

Use same boost:thread variable to create multiple threads

Is std::this_thread::yield any different from sched_yield on linux?

How to interrupt boost::this_thread in c++

Best practice: should I use std::thread, Boost, or native calls?

std::this_thread::sleep_for() freezing the program

How to manage concurrence reading from a file descripter using multiple boost::thread objests. (boost threads management)

Is it okay to use one angular service from another?

Is it okay to use attributes from a tag in another tag?

Calling boost::asio::io_service::run from a std::thread

std::thread class vs std::this_thread namespace in c++?

c++ thread pool: alternative to std::function for passing functions/lambdas to threads?

Is it okay to inherit from an std::variant in order to make it recursive?

Setting Thread Affinity of std::threads

When to use std::async vs std::threads?

std::condition_variable not working with std::this_thread::sleep_for()

Use boost strand and std::mutex

C++ multiple callback member functions from different classes without std and boost

Is boost's io_service share threads for request when called from thread pool?

Interrupt all threads in the Boost Asio Thread Pool

Is it okay to use Thread.sleep as a timer in a console application?

Code generated - usleep vs std::this_thread::sleep_for

What is s in std::this_thread::sleep_for(2s)?

Can std::this_thread::sleep_for() have spurious wakeups?

std::this_thread::sleep_for sleeps for too long

std::this_thread::sleep_for() vs sleep() in main()

Is it okay to call a method which use variables from another method in a class?

Why is `std::this_thread::yield()` 10x slower than `std::this_thread::sleep_for(0s)`?