A shared recursive mutex in standard C++

Recursive property of the mutex operates with the term “owner“, which in case of a shared_mutex is not well-defined: several threads may have .lock_shared() called at the same time. Assuming “owner” to be a thread which calls .lock() (not .lock_shared()!), an implementation of the recursive shared mutex can be simply derived from shared_mutex: class shared_recursive_mutex: … Read more

What’s the difference between first locking and creating a lock_guard(adopt_lock) and creating a unique_lock(defer_lock) and locking?

1) First code sample { static std::mutex io_mutex; std::lock_guard<std::mutex> lk(io_mutex); std::cout << e1.id << ” and ” << e2.id << ” are waiting for locks” << std::endl; } This is a standard lock guard, when the scope is exited, the lock lk is released { std::unique_lock<std::mutex> lk1(e1.m, std::defer_lock); std::unique_lock<std::mutex> lk2(e2.m, std::defer_lock); std::lock(lk1, lk2); std::cout << … Read more

Difference between Mutex, Semaphore & Spin Locks

First, remember the goal of these ‘synchronizing objects’ : These objects were designed to provide an efficient and coherent use of ‘shared data’ between more than 1 thread among 1 process or from different processes. These objects can be ‘acquired’ or ‘released’. That is it!!! End of story!!! Now, if it helps to you, let … Read more

How to implement a unmanaged thread-safe collection when I get this error: is not supported when compiling with /clr

It is not supported because the std::mutex implementation uses GetCurrentThreadId(). That’s a winapi function that is not supposed to be use in managed code since it might be running on a custom CLR host that doesn’t use threads to implement threading. This is the good kind of problem to have, it shows that you are … Read more

Does std::mutex create a fence?

As I understand this is covered in: 1.10 Multi-threaded executions and data races Para 5: The library defines a number of atomic operations (Clause 29) and operations on mutexes (Clause 30) that are specially identified as synchronization operations. These operations play a special role in making assignments in one thread visible to another. A synchronization … Read more

How can I tell if another instance of my program is already running?

As Jon first suggested, you can try creating a mutex. Call CreateMutex. If you get a non-null handle back, then call GetLastError. It will tell you whether you were the one who created the mutex or whether the mutex was already open before (Error_Already_Exists). Note that it is not necessary to acquire ownership of the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)