views:

59

answers:

4

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?

UPDATE

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

A: 

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.

WhirlWind
A: 

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.

Mark M
WhirlWind I think you misunderstood my response. I agree if two threads are sharing a resource then there may be a need for mutual exclusion. My understanding of the question was, if there is no shared resource but one thread can't begin something until another finishes something else then synchronization is required not mutual exclusion.
Mark M
@Mark I think I did too; I deleted my comment a bit ago. +1 for a nice explanation.
WhirlWind
Guys,I've updated the question.Sorry for not clarifying it the first time.
httpinterpret
@httpinterpret No, it won't crash. Threads may or may not "signal" as you expect.
WhirlWind
The only drawback is time delay,right?
httpinterpret
@httpinterpret probably... why don't you just use an atomic variable to make sure things work correctly? A bit of care and foresight in code implementation and design can save you a big headache later.
WhirlWind
By the way, you might also consider a condition variable for this.
WhirlWind
A: 

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.

Jerry Coffin
+1  A: 

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.

Jay