Re: read-write mutex
- From: David Schwartz <davids@xxxxxxxxxxxxx>
- Date: Fri, 9 Jan 2009 11:48:40 -0800 (PST)
On Jan 9, 9:08 am, snovgorod...@xxxxxxxxx wrote:
On Jan 8, 3:47 pm, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:> On Jan 8, 9:05 am, snovgorod...@xxxxxxxxx wrote:
I guess my question is why would I need a mutex in case of multiple
readers/single writer?
Because the pthreads standard says that the results of one thread
reading from a variable while another thread is or might be modifying
it is undefined.
Could you plz give me an example of such undefined result?
The program crashes. The computer catches on fire. Your dog gets
pregnant.
An "undefined result" means that anything at all can happen without
violating the standard.
I assumed that 'undefined' in this case means my reader threads
either
still read zero from the var or read some value which may be a result
of an incomplete write(var!=0).
Your assumption may be correct for some particular combination of
hardware, compiler, system libraries and so on.
Both cases are acceptable for my app, that's why I didn't use the
mutex.
I tried to avoid any decrease in performance , I thought (may be
incorrectly) that I would slow down my app, running a hundred threads
that read the variable often
and do lock/unlock every time.
That sounds like a bad design. Maybe you should fix the design so you
don't have such contention hotspots. If proper locking is impacting
performance, there's a good chance the architecture is bad.
DS
.
- References:
- read-write mutex
- From: snovgorodsky
- Re: read-write mutex
- From: David Schwartz
- Re: read-write mutex
- From: snovgorodsky
- read-write mutex
- Prev by Date: Re: read-write mutex
- Next by Date: Re: read-write mutex
- Previous by thread: Re: read-write mutex
- Next by thread: Memory barrier + volatile (C++)
- Index(es):
Relevant Pages
|