Re: Mutex or atomic operations for multithread app?



On Sat, 2006-11-11 at 14:29 +0100, Tomasz Jankowski wrote:

I looking for someone, who will say if my idea is bad or good ;)
I have an object based on GObject, which have quite many int variables. This
object is used by many threads and i'm wondering if I really need to use
mutex when I want to access those variables. Can I use atomically access to
int variables using g_atomic* functions instead using mutex? Is it a good
solution or it may occur memory access problems?

No, it is not a good solution.  If you don't know what memory ordering,
barriers, etc. is then you don't know enough to protect yourself against
the countless subtle ways of doing this wrong.

Just use the mutex.

The real problem you have is that your threads communicate too much
through shared variables.  Make them communicate less instead of doing
the equivalent of removing all the safety shields on your high powered
saw.

-Peter




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