using boost/detail/atomic_count.hpp and scope locking

hi! class someclass { public: bool a_writer(...); bool a_reader(...); bool another_reader(...); .... private: .... boost::mutex mutex; boost::detail::atomic_count locked; }; someclass::someclass():locked(0) { } bool someclass::a_writer(.....) { boost::mutex::scoped_lock scoped_lock(mutex,true); ++locked; if(something) { ........... --locked; return true; } ............ --locked; return false; } bool someclass::a_reader(.....) { boost::mutex::scoped_lock scoped_lock(mutex,(locked!=0)); if(something) { ......... return true; } ............ return false; } bool someclass::another_reader(.....) { boost::mutex::scoped_lock scoped_lock(mutex,(locked!=0)); if(something) { ........... return true; } ............ return false; } I wanna combine atomic_count and scope locking so I can avoid locking on every operation. writers will get the lock,blocking all others,readers is able to go on. the method a_writer(....) is not used very often... I know that what I am doing is not recommended,atomic_count.hpp is for internal use only, but is so usefull!! would this work as I expect? does it worth it? thanks!

writers will get the lock,blocking all others,readers is able to go on.
sorry! I must explain more... a writer will get the lock,blocking all others,reader(s) is able to go on if there is no writer. thanks.

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

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.
yes,it's risky.....
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.
yes,I acquire the lock when and only when the locked==0,the time I check for it...offcourse a shoooort time after it may be changed by a writer and here is a problem........but there is 3 things : a)The readers method is non-blocking,in memory searching in std::map,very fast b)writer method will be used very rare,every 24hrs c)I don't sugest this technik in general,just for this case,almost read only access. I just post this code becouse I wanted boost developers opinion about what I am doing and get response from if they had did anything similar.
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.
there is no read-write lock anymore in boost-thread :-( read-write lock are very expensive and in my case would be worse....
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.
I am happy to hear this:) thanks.
participants (2)
-
george
-
Ulrich Eckhardt