Re: Question about multithreads



On 10/31/06, Tomasz Jankowski <tomcioj gmail com> wrote:
wrote works fine). However I'm not sure if I should always lock and unlock
mutex when I'm accesing some global data from thread. I know, that i'm

If the value is changing and the changes are being used to signal
between threads, then you need to have some thread synchronisation so
that the various CPU caches can get in line.

If the value doesn't change, or you don't mind if changes are not seen
by other threads for a while, then it's OK to skip the mutex.

It can sometimes be difficult to decide though. What if you skip a
mutex because you don't care about out-of-date values, and then later
change something somewhere else and those (complicated!) assumptions
break? You've got a very hard to find race condition that will
randomly crash your program.

In my opinion, the solution is to keep communication between threads
to a minimum, and where they must communicate, you keep the point of
communication small and carefully protected.

John



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]