When should we use mutex and when should we use semaphore

Here is how I remember when to use what – Semaphore: Use a semaphore when you (thread) want to sleep till some other thread tells you to wake up. Semaphore ‘down’ happens in one thread (producer) and semaphore ‘up’ (for same semaphore) happens in another thread (consumer) e.g.: In producer-consumer problem, producer wants to sleep … Read more

Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?

There are several differences between Boost.Thread and the C++11 standard thread library: Boost supports thread cancellation, C++11 threads do not C++11 supports std::async, but Boost does not Boost has a boost::shared_mutex for multiple-reader/single-writer locking. The analogous std::shared_timed_mutex is available only since C++14 (N3891), while std::shared_mutex is available only since C++17 (N4508). C++11 timeouts are different … Read more

What are the differences between various threading synchronization options in C#?

Great question. I maybe wrong.. Let me try.. Revision#2 of my orig answer.. with a little bit of more understanding. Thanks for making me read 🙂 lock(obj) is a CLR construct that for (intra-object?) thread synchronization. Ensures that only one thread can take ownership of the object’s lock & enter the locked block of code. … Read more

How efficient is locking and unlocked mutex? What is the cost of a mutex?

I have the choice in between either having a bunch of mutexes or a single one for an object. If you have many threads and the access to the object happens often, then multiple locks would increase parallelism. At the cost of maintainability, since more locking means more debugging of the locking. How efficient is … Read more

Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex)

The difference between a recursive and non-recursive mutex has to do with ownership. In the case of a recursive mutex, the kernel has to keep track of the thread who actually obtained the mutex the first time around so that it can detect the difference between recursion vs. a different thread that should block instead. … Read more

Concurrent HashSet in .NET Framework?

Your implementation is correct. The .NET Framework does not provide a built-in concurrent hashset type, unfortunately. However, there are some workarounds. ConcurrentDictionary (recommended) This first one is to use the class ConcurrentDictionary<TKey, TValue> in the namespace System.Collections.Concurrent. In the case, the value is pointless, so we can use a simple byte (1 byte in memory). … Read more

Why do pthreads’ condition variable functions require a mutex?

It’s just the way that condition variables are (or were originally) implemented. The mutex is used to protect the condition variable itself. That’s why you need it locked before you do a wait. The wait will “atomically” unlock the mutex, allowing others access to the condition variable (for signalling). Then when the condition variable is … Read more

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