Thread safety of nested calls - c++

I have two libs, one is thread safe called class A, The other lib called class B, which used class A to realize functions.
class A {
public:
void Get() {
std::lock_guard<std::mutex> lock(mutex_);
do_something
}
void Put() {
std::lock_guard<std::mutex> lock(mutex_);
do_something
}
private:
std::mutex mutex_;
};
class B {
public:
void Get() {
a.Get();
}
void Put() {
a.Put();
}
private:
A a;
};
So is class B thread safe?
I know that judging whether the thread is safe depends on whether the operation is atomic. If the put operate is not atomic then it's not thread safe. According to the above requirements, I think class B is not an atomic operation, so it is not thread-safe?
When the operation is not atomic, it may not be thread safe. for example we add some operate like below, Is it right?
class B {
public:
void Get() { // But Get is not atomic!!!
do_some_thing(); // atomic
a.Get(); // atomic
do_some_thing(); // atomic
}
void Put() {
do_some_thing();
a.Put();
do_some_thing();
}
private:
A a;
};

Thread safety concerns about the race conditions and data races.
Now, Since the methods of class B don't use any data directly but via delegating other methods in class A that as you said are thread-safe, the methods in B are thread-safe.

Related

Avoid destruction of object when executing futures in different threads

Imagine the following situation:
class A {
public:
folly::Future<folly::Unit> fooA(std::function<void()> callback);
};
class B {
public:
void fooB() {
a_->fooA([] { doSomethingCheap_(); }) /* Executed in thread 1 */
.via(exec_.get())
.then([] { doSomethingExpensive_(); }) /* Executed in thread 2 */
}
private:
std::shared_ptr<folly::Executor> exec_;
std::shared_ptr<A> a_;
void doSomethingCheap_();
void doSomethingExpensive_();
};
If at the time we end executing doSomethingCheap_() object B b will be destroyed then we will get segfault. Probably we can hold weak_ptr<B> in class A, but this approach is not extensible when we want to use class A not only in class B but also in some class C, ...
What is the best way avoiding it?
I'm not familiar with folly or what synchronization mechanisms you're using, but it seems like you could maybe use a Mutex-guarded bool that you capture and pass to the lambda calling doSomethingExpensive - this would be a "poor-man's join". Lock the mutex and then flip the bool to true. Alternately, you could use something like absl::Notification [since that what I know].
#include "absl/synchronization/notification.h"
class A {
public:
folly::Future<folly::Unit> fooA(std::function<void()> callback);
};
class B {
public:
void fooB() {
a_->fooA([] { doSomethingCheap_(); }) /* Executed in thread 1 */
.via(exec_.get())
.then([this] {
doSomethingExpensive_();
finished_.Notify();
}) /* Executed in thread 2 */
finished_.WaitForNotification();
}
private:
std::shared_ptr<folly::Executor> exec_;
std::shared_ptr<A> a_;
absl::Notification finished_;
void doSomethingCheap_();
void doSomethingExpensive_();
};
Ultimately, joining on the threads seems like the right way to go, I'm just not sure what is exposed in folly.

Thread-safe and unique execution of nested function

I have a class MyClass whose function A is executed many times in parallel. Then, there is function B that should only be executed once. My initial setup looks simple but I doubt that it is thread-safe. How can I make it thread-safe? I'm using C++11.
class MyClass {
public:
void A() {
static bool execute_B = true;
if (execute_B) {
execute_B = false;
B();
}
}
private:
void B() {
std::cout << "only execute this once\n";
}
};
This is a primary use-case for std::atomic_flag:
class MyClass {
public:
void A() {
if (!execute_B_.test_and_set()) {
B();
}
}
private:
void B() {
std::cout << "only execute this once\n";
}
std::atomic_flag execute_B_ = ATOMIC_FLAG_INIT;
};
Online Demo
Note that any solutions involving static will allow only one invocation of MyClass::B, even across multiple MyClass instances, which may or may not make sense for you; assuming it doesn't make sense, this approach instead allows one invocation of MyClass::B per MyClass instance.
Yes, your code is not thead-safe: several threads can enter inside the body of if statement before execute_B will be set to false. Also, execute_B is not atomic, so you can have problems with visibility of changes between threads.
There are many ways you can make it thread-safe. Note that version (1), (2) and (4) will block other thread from executing A past the point of B execution, until B execution is finished.
1) Already mentioned std::call_once:
void A() {
static std::once_flag execute_B;
std::call_once(flag1, [this](){ B(); });
}
2) Calling B as result of initializating static variable:
void A() {
static bool dummy = [this](){ B(); return true; });
}
3) Using atomic exchange:
void A() {
static std::atomic<bool> execute_B = true;
if(execute_B.exchange(false, std::memory_order_acq_rel))
B();
}
4) Protecting check with a mutex (to avoid perfomance degradation later, use double-checked locking):
void A() {
static std::mutex m_;
static std::atomic<bool> execute_B = true;
if(execute_B.load(std::memory_order_acquire)) {
std::unique_lock<std::mutex> lk(m_);
if(execute_B.load(std::memory_order_relaxed)) {
B();
execute_B.store(false, std::memory_order_release);
}
}
}

C++ on singleton

I've a singleton class and I'm sure that the first call of the singleton is done by only one thread. I've implemented singleton with lazy initialization.
class MySingleton : private boost::noncopyable {
public:
/** singleton access. */
static MySingleton & instance()
{
static MySingleton myInstance;
return myInstance;
}
void f1();
void f2();
void f3();
void f4();
private:
MySingleton();
};
Now I've another factory class that is responsable to create all singletons in a single thread enviorment.
The singleton can be used from multiple threads and methods are protected from mutex.
First question
Is this approach accetable?
Second question
I've a complex class that must be thread safe. This class has to be a singleton. How can that a calling of different methods of the class is thread safe. For example.
{
MySingletonLock lock;
// Other thread must wait here.
MySingleton::instance().f1();
MySingleton::instance().f3();
}
How can I get this?
The answer to your second question:
class MyProtectedSingleton: public MySingleton
{
public:
void f1()
{
MySingletonLock lock;
// Other thread must wait here.
MySingleton::instance().f1();
}
void f2()
{
MySingletonLock lock;
// Other thread must wait here.
MySingleton::instance().f2();
}
};
Call f1, f2, etc through wrappers in MyProtectedSingleton.

custom RAII C++ implementation for scoped mutex locks

I cannot use boost or the latest std::thread library. The way to go is to create a custom implementation of a scoped mutex.
In a few words when a class instance is create a mutex locks. Upon class destruction the mutex is unlocked.
Any implementation available? I don't want to re-invent the wheel.
I need to use pthreads.
resource acquisition is initialization == “RAII”
Note This is an old answer. C++11 contains better helpers that are more platform independent:
std::lock_guard
std::mutex, std::timed_mutex, std::recursive_mutex, std::recursive_timed_mutex
And other options like std::unique_lock, boost::unique_lock
Any RAII tutorial will do.
Here's the gist: (also on http://ideone.com/kkB86)
// stub mutex_t: implement this for your operating system
struct mutex_t
{
void Acquire() {}
void Release() {}
};
struct LockGuard
{
LockGuard(mutex_t& mutex) : _ref(mutex)
{
_ref.Acquire(); // TODO operating system specific
}
~LockGuard()
{
_ref.Release(); // TODO operating system specific
}
private:
LockGuard(const LockGuard&); // or use c++0x ` = delete`
mutex_t& _ref;
};
int main()
{
mutex_t mtx;
{
LockGuard lock(mtx);
// LockGuard copy(lock); // ERROR: constructor private
// lock = LockGuard(mtx);// ERROR: no default assignment operator
}
}
Of course you can make it generic towards mutex_t, you could prevent subclassing.
Copying/assignment is already prohibited because of the reference field
EDIT For pthreads:
struct mutex_t
{
public:
mutex_t(pthread_mutex_t &lock) : m_mutex(lock) {}
void Acquire() { pthread_mutex_lock(&m_mutex); }
void Release() { pthread_mutex_unlock(&m_mutex); }
private:
pthread_mutex_t& m_mutex;
};

Select mutex or dummy mutex at runtime

I have a class that is shared between several projects, some uses of it are single-threaded and some are multi-threaded. The single-threaded users don't want the overhead of mutex locking, and the multi-threaded users don't want to do their own locking and want to be able to optionally run in "single-threaded mode." So I would like to be able to select between real and "dummy" mutexes at runtime.
Ideally, I would have a shared_ptr<something> and assign either a real or fake mutex object. I would then "lock" this without regard to what's in it.
unique_lock<something> guard(*mutex);
... critical section ...
Now there is a signals2::dummy_mutex but it does not share a common base class with boost::mutex.
So, what's an elegant way to select between a real mutex and a dummy mutex (either the one in signals2 or something else) without making the lock/guard code more complicated than the example above?
And, before you point out the alternatives:
I could select an implementation at compile time, but preprocessor macros are ugly and maintaining project configurations is painful for us.
Users of the class in a multi-threaded environment do not want to take on the responsibility of locking the use of the class rather than having the class do its own locking internally.
There are too many APIs and existing usages involved for a "thread-safe wrapper" to be a practical solution.
How about something like this?
Its untested but should be close to OK.
You might consider making the template class hold a value rather than a pointer
if your mutexes support the right kinds of constructions. Otherwise you could specialise the MyMutex class to get value behaviour.
Also it's not being careful about copying or destruction .. I leave that as an exercise to the reader ;) ( shared_ptr or storing a value rather than a pointer should fix this)
Oh and the code would be nicer using RAII rather than explicit lock/unlock... but that's a different question.I assume thats what the unique_lock in your code does?
struct IMutex
{
virtual ~IMutex(){}
virtual void lock()=0;
virtual bool try_lock()=0;
virtual void unlock()=0;
};
template<typename T>
class MyMutex : public IMutex
{
public:
MyMutex(T t) : t_(t) {}
void lock() { t_->lock(); }
bool try_lock() { return t_->try_lock(); }
void unlock() { t_->unlock(); }
protected:
T* t_;
};
IMutex * createMutex()
{
if( isMultithreaded() )
{
return new MyMutex<boost::mutex>( new boost::mutex );
}
else
{
return new MyMutex<signal2::dummy_mutex>( new signal2::dummy_mutex );
}
}
int main()
{
IMutex * mutex = createMutex();
...
{
unique_lock<IMutex> guard( *mutex );
...
}
}
Since the two mutex classes signals2::dummy_mutex and boost::mutex don't share a common base class you could use something like "external polymorphism" to allow to them to be treated polymorphically. You'd then use them as locking strategies for a common mutex/lock interface. This allows you to avoid using "if" statements in the lock implementation.
NOTE: This is basically what Michael's proposed solution implements. I'd suggest going with his answer.
Have you ever heard about Policy-based Design ?
You can define a Lock Policy interface, and the user may choose which policy she wishes. For ease of use, the "default" policy is precised using a compile-time variable.
#ifndef PROJECT_DEFAULT_LOCK_POLICY
#define PROJECT_DEFAULT_LOCK_POLICY TrueLock
#endif
template <class LP = PROJECT_DEFAULT_LOCK_POLICY>
class MyClass {};
This way, your users can choose their policies with a simple compile-time switch, and may override it one instance at a time ;)
This is my solution:
std::unique_lock<std::mutex> lock = dummy ?
std::unique_lock<std::mutex>(mutex, std::defer_lock) :
std::unique_lock<std::mutex>(mutex);
Is this not sufficient?
class SomeClass
{
public:
SomeClass(void);
~SomeClass(void);
void Work(bool isMultiThreaded = false)
{
if(isMultiThreaded)
{
lock // mutex lock ...
{
DoSomething
}
}
else
{
DoSomething();
}
}
};
In general, a mutex is only needed if the resource is shared between multiple processes. If an instance of the object is unique for a (possibly multi-threaded) process, then a Critical Section is often more appropriate.
In Windows, the single-threaded implementation of a Critical Section is a dummy one. Not sure what platform you are using.
Just FYI, here's the implementation I ended up with.
I did away with the abstract base class, merging it with the no-op "dummy" implementation. Also note the shared_ptr-derived class with an implicit conversion operator. A little too tricky, I think, but it lets me use shared_ptr<IMutex> objects where I previously used boost::mutex objects with zero changes.
header file:
class Foo {
...
private:
struct IMutex {
virtual ~IMutex() { }
virtual void lock() { }
virtual bool try_lock() { return true; }
virtual void unlock() { }
};
template <typename T> struct MutexProxy;
struct MutexPtr : public boost::shared_ptr<IMutex> {
operator IMutex&() { return **this; }
};
typedef boost::unique_lock<IMutex> MutexGuard;
mutable MutexPtr mutex;
};
implementation file:
template <typename T>
struct Foo::MutexProxy : public IMutex {
virtual void lock() { mutex.lock(); }
virtual bool try_lock() { return mutex.try_lock(); }
virtual void unlock() { mutex.unlock(); }
private:
T mutex;
};
Foo::Foo(...) {
mutex.reset(single_thread ? new IMutex : new MutexProxy<boost::mutex>);
}
Foo::Method() {
MutexGuard guard(mutex);
}
Policy based Option:
class SingleThreadedPolicy {
public:
class Mutex {
public:
void Lock() {}
void Unlock() {}
bool TryLock() { return true; }
};
class ScopedGuard {
public:
ScopedGuard(Mutex& mutex) {}
};
};
class MultithreadingPolicy {
public:
class ScopedGuard;
class Mutex {
friend class ScopedGuard;
private:
std::mutex mutex_;
public:
void Lock() {
mutex_.lock();
}
void Unlock() {
mutex_.unlock();
}
bool TryLock() {
return mutex_.try_lock();
}
};
class ScopedGuard {
private:
std::lock_guard<std::mutex> lock_;
public:
ScopedGuard(Mutex& mutex) : lock_(mutex.mutex_) {}
};
};
Then it can be used as follows:
template<class ThreadingPolicy = SingleThreadedPolicy>
class MyClass {
private:
typedef typename ThreadingPolicy::Mutex Mutex;
typedef typename ThreadingPolicy::ScopedGuard ScopedGuard;
Mutex mutex_;
public:
void DoSomething(){
ScopedGuard guard(mutex_);
std::cout<<"Hello World"<<std::endl;
}
};