Map with concurrent access

Multiple readers, no writers is okay: https://groups.google.com/d/msg/golang-nuts/HpLWnGTp-n8/hyUYmnWJqiQJ One writer, no readers is okay. (Maps wouldn’t be much good otherwise.) Otherwise, if there is at least one writer and at least one more either writer or reader, then all readers and writers must use synchronization to access the map. A mutex works fine for this.

Calling pthread_cond_signal without locking mutex

If you do not lock the mutex in the codepath that changes the condition and signals, you can lose wakeups. Consider this pair of processes: Process A: pthread_mutex_lock(&mutex); while (condition == FALSE) pthread_cond_wait(&cond, &mutex); pthread_mutex_unlock(&mutex); Process B (incorrect): condition = TRUE; pthread_cond_signal(&cond); Then consider this possible interleaving of instructions, where condition starts out as FALSE: … Read more

How should I deal with mutexes in movable types in C++?

Let’s start with a bit of code: class A { using MutexType = std::mutex; using ReadLock = std::unique_lock<MutexType>; using WriteLock = std::unique_lock<MutexType>; mutable MutexType mut_; std::string field1_; std::string field2_; public: … I’ve put some rather suggestive type aliases in there that we won’t really take advantage of in C++11, but become much more useful in … Read more

Which is more efficient, basic mutex lock or atomic integer?

Atomic operations leverage processor support (compare and swap instructions) and don’t use locks at all, whereas locks are more OS-dependent and perform differently on, for example, Win and Linux. Locks actually suspend thread execution, freeing up cpu resources for other tasks, but incurring in obvious context-switching overhead when stopping/restarting the thread. On the contrary, threads … Read more

What is mutex and semaphore in Java ? What is the main difference?

Unfortunately everyone has missed the most important difference between the semaphore and the mutex; the concept of “ownership“. Semaphores have no notion of ownership, this means that any thread can release a semaphore (this can lead to many problems in itself but can help with “death detection”). Whereas a mutex does have the concept of … Read more

Example for boost shared_mutex (multiple reads/one write)?

1800 INFORMATION is more or less correct, but there are a few issues I wanted to correct. boost::shared_mutex _access; void reader() { boost::shared_lock< boost::shared_mutex > lock(_access); // do work here, without anyone having exclusive access } void conditional_writer() { boost::upgrade_lock< boost::shared_mutex > lock(_access); // do work here, without anyone having exclusive access if (something) { … Read more

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