|
Boost : |
From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2006-04-03 16:13:22
On Sunday 02 April 2006 22:25, george wrote:
> boost::mutex mutex;
> boost::detail::atomic_count locked;
[...]
> bool someclass::a_writer(.....)
> {
> boost::mutex::scoped_lock scoped_lock(mutex,true);
[point 1]
> ++locked;
[...]
> --locked;
> return false;
> }
Note here: if a different thread is scheduled at point 1, it will not see that
'locked' will be incremented, only the locked mutex.
> bool someclass::a_reader(.....)
> {
> boost::mutex::scoped_lock scoped_lock(mutex,(locked!=0));
Here, if 'locked' is not zero, you acquire the mutex. Well, to be more
precise, you acquire the mutex if at some -albeit short ago- time in the past
'locked' was not zero. You don't acquire it if at some equally long time in
the past it was zero. You have no idea about whether it is zero now.
> I wanna combine atomic_count and scope locking so I can avoid locking on
> every operation.
Hmm, what you want is a read-write lock, it seems. Anyhow, how to do this
right has nothing to do on the Boost development mailinglist I would say.
Rather, you should take this to e.g. comp.programming.threads, which is an
excellent resource as several people that practise lock-free programming
(which is to some extent what you are trying, too) are regulars there.
> I know that what I am doing is not recommended,atomic_count.hpp is for
> internal use only, but is so usefull!
Formalising and exporting atomic integer operations was requested before, +1
to the number of votes for that, or rather +2 if I may give my vote. I think
this has already been scheduled to after the rewrite of Boost.Threads though.
Uli
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk