• Uncategorized

About multithreading : Is-is-necessary-to-create-a-Mutex-for-a-read-only-thread-and-a-write-only-thread

Question Detail

There are 2 threads,one only reads the signal,the other only sets the signal.

Is it necessary to create a mutex for signal and the reason?


All I care is whether it’ll crash if two threads read/set the same time

Question Answer

You will probably want to use atomic variables for this, though a mutex would work as well.

The problem is that there is no guarantee that data will stay in sync between threads, but using atomic variables ensures that as soon as one thread updates that variable, other threads immediately read its updated value.

A problem could occur if one thread updates the variable in cache, and a second thread reads the variable from memory. That second thread would read an out-of-date value for the variable, if the cache had not yet been flushed to memory. Atomic variables ensure that the value of the variable is consistent across threads.

If you are not concerned with timely variable updates, you may be able to get away with a single volatile variable.

It depends. If writes are atomic then you don’t need a mutual exclusion lock. If writes are not atomic, then you do need a lock.

There is also the issue of compilers caching variables in the CPU cache which may cause the copy in main memory to not get updating on every write. Some languages have ways of telling the compiler to not cache a variable in the CPU like that (volatile keyword in Java), or to tell the compiler to sync any cached values with main memory (synchronized keyword in Java). But, mutex’s in general don’t solve this problem.

If all you need is synchronization between threads (one thread must complete something before the other can begin something else) then mutual exclusion should not be necessary.

Mutual exclusion is only necessary when threads are sharing some resource where the resource could be corrupted if they both run through the critical section at roughly the same time. Think of two people sharing a bank account and are at two different ATM’s at the same time.

Depending on your language/threading library you may use the same mechanism for synchronization as you do for mutual exclusion- either a semaphore or a monitor. So, if you are using Pthreads someone here could post an example of synchronization and another for mutual exclusion. If its java, there would be another example. Perhaps you can tell us what language/library you’re using.

If, as you’ve said in your edit, you only want to assure against a crash, then you don’t need to do much of anything (at least as a rule). If you get a collision between threads, about the worst that will happen is that the data will be corrupted — e.g., the reader might get a value that’s been partially updated, and doesn’t correspond directly to any value the writing thread ever wrote. The classic example would be a multi-byte number that you added something to, and there was a carry, (for example) the old value was 0x3f ffff, which was being incremented. It’s possible the reading thread could see 0x3f 0000, where the lower 16 bits have been incremented, but the carry to the upper 16 bits hasn’t happened (yet).

On a modern machine, an increment on that small of a data item will normally be atomic, but there will be some size (and alignment) where it’s not — typically if part of the variable is in one cache line, and part in another, it’ll no longer be atomic. The exact size and alignment for that varies somewhat, but the basic idea remains the same — it’s mostly just a matter of the number having enough digits for it to happen.

Of course, if you’re not careful, something like that could cause your code to deadlock or something on that order — it’s impossible to guess what might happen without knowing anything about how you plan to use the data.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.