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

C++0x has no semaphores? How to synchronize threads?

You can easily build one from a mutex and a condition variable: #include <mutex> #include <condition_variable> class semaphore { std::mutex mutex_; std::condition_variable condition_; unsigned long count_ = 0; // Initialized as locked. public: void release() { std::lock_guard<decltype(mutex_)> lock(mutex_); ++count_; condition_.notify_one(); } void acquire() { std::unique_lock<decltype(mutex_)> lock(mutex_); while(!count_) // Handle spurious wake-ups. condition_.wait(lock); –count_; } bool … Read more

How to wait for all goroutines to finish without using time.Sleep?

You can use sync.WaitGroup. Quoting the linked example: package main import ( “net/http” “sync” ) func main() { var wg sync.WaitGroup var urls = []string{ “http://www.golang.org/”, “http://www.google.com/”, “http://www.somestupidname.com/”, } for _, url := range urls { // Increment the WaitGroup counter. wg.Add(1) // Launch a goroutine to fetch the URL. go func(url string) { // … Read more

Conditional Variable vs Semaphore

Locks are used for mutual exclusion. When you want to ensure that a piece of code is atomic, put a lock around it. You could theoretically use a binary semaphore to do this, but that’s a special case. Semaphores and condition variables build on top of the mutual exclusion provide by locks and are used … 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

Asynchronous Process inside a javascript for loop [duplicate]

The for loop runs immediately to completion while all your asynchronous operations are started. When they complete some time in the future and call their callbacks, the value of your loop index variable i will be at its last value for all the callbacks. This is because the for loop does not wait for an … Read more

How do synchronized static methods work in Java and can I use it for loading Hibernate entities?

To address the question more generally… Keep in mind that using synchronized on methods is really just shorthand (assume class is SomeClass): synchronized static void foo() { … } is the same as static void foo() { synchronized(SomeClass.class) { … } } and synchronized void foo() { … } is the same as void foo() … Read more

How does @synchronized lock/unlock in Objective-C?

The Objective-C language level synchronization uses the mutex, just like NSLock does. Semantically there are some small technical differences, but it is basically correct to think of them as two separate interfaces implemented on top of a common (more primitive) entity. In particular with a NSLock you have an explicit lock whereas with @synchronized you … Read more

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