Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 234
Claudiu Avatar asked Apr 17 '15 21:04

Claudiu


1 Answers

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

like image 186
sehe Avatar answered Oct 14 '22 07:10

sehe