Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » StaticMutex/ONCELOCK question
icon5.gif  StaticMutex/ONCELOCK question [message #19927] Tue, 03 February 2009 06:28 Go to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
I couldn't understand completely several things with StaticMutex and ONCELOCK.

StaticMutex will never call destructor of a contained Mutex object. Is this meant to be?

#define ONCELOCK \
for(static volatile bool o_b_; !ReadWithBarrier(o_b_);) \
	for(static StaticMutex o_ss_; !o_b_;) \
		for(Mutex::Lock o_ss_lock__(o_ss_); !o_b_; BarrierWrite(o_b_, true))


How the above code actually works? Rolling Eyes

TIA


Regards,
Novo
Re: StaticMutex/ONCELOCK question [message #19928 is a reply to message #19927] Tue, 03 February 2009 07:41 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Tue, 03 February 2009 00:28

I couldn't understand completely several things with StaticMutex and ONCELOCK.

StaticMutex will never call destructor of a contained Mutex object. Is this meant to be?



Yes. OS will clean that up when program exits.

Quote:


#define ONCELOCK \
for(static volatile bool o_b_; !ReadWithBarrier(o_b_);) \
	for(static StaticMutex o_ss_; !o_b_;) \
		for(Mutex::Lock o_ss_lock__(o_ss_); !o_b_; BarrierWrite(o_b_, true))


How the above code actually works? Rolling Eyes

TIA



Do not get fooled by 3 'for' loops - these are just syntactic sugar to make ONCELOCK work on C statements and blocks - they in fact simulate the outer block

{
   static volatile bool o_b_;
   if(!ReadWithBarrier(o_b_)) {
       static StaticMutex mutex;
       mutex.Enter();
       {
           do_the_initialization - the statement 'body'
           BarrierWrite(o_b_);
       }
   }
}


The purpose is to avoid locking mutex in subsequent passes of ONCELOCK - you need the barrier code to do that.

Note that both compilers we use optimize the for loops away.

Mirek
Re: StaticMutex/ONCELOCK question [message #19940 is a reply to message #19928] Tue, 03 February 2009 20:28 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
luzr wrote on Tue, 03 February 2009 01:41


Do not get fooled by 3 'for' loops - these are just syntactic sugar to make ONCELOCK work on C statements and blocks - they in fact simulate the outer block



Thanks. I understand the idea with loops. I'm using similar technique to handle transactions myself.

What I do not understand is how uninitialized o_b_ works.

{
   static volatile bool o_b_;
   if(!ReadWithBarrier(o_b_)) {
       ...
   }
}




Regards,
Novo
Re: StaticMutex/ONCELOCK question [message #19984 is a reply to message #19940] Sun, 08 February 2009 03:09 Go to previous message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
Novo wrote on Tue, 03 February 2009 14:28


What I do not understand is how uninitialized o_b_ works.

{
   static volatile bool o_b_;
   if(!ReadWithBarrier(o_b_)) {
       ...
   }
}





I finally figured out that myself. ANSI-compatible compiler initializes all static POD data with zeroes before a very first function call. So, this static variable is always initialized in a thread-safe way.

There is always something to learn about C.


Regards,
Novo
Previous Topic: HTTP Agent & WebSSL memory leak
Next Topic: Thread calls GUI
Goto Forum:
  


Current Time: Fri Mar 29 10:26:03 CET 2024

Total time taken to generate the page: 0.01463 seconds