[omniORB] omni_mutex::trylock

Alex Tingle alex.tingle at bronermetals.com
Tue Sep 9 15:53:40 BST 2008


Why not add a helper class analogous to omni_mutex_lock? Something like:

class _OMNITHREAD_NTDLL_ omni_mutex_trylock {
     omni_mutex& mutex;
public:
     const int locked;
     omni_mutex_trylock(omni_mutex& m) : mutex(m),  
locked(mutex.trylock()) {}
     ~omni_mutex_trylock(void) { if(locked) mutex.unlock(); }
     operator int(void) const { return locked; }
private:
     // dummy copy constructor and operator= to prevent copying
     omni_mutex_trylock(const omni_mutex_trylock&);
     omni_mutex_trylock& operator=(const omni_mutex_trylock&);
};


It would be used like this:

{
   omni_mutex_trylock lock(m);
   if(lock)
   {
     // Got mutex.
   }
   else
   {
     // Failed to get mutex.
   }
}


If you don't like the cast overloading, then you could just use the  
public const member...

   if(lock.locked) ...


regards,

-Alex

--
On 9 Sep 2008, at 07:22, Luke Deller wrote:

> [resending with the attachment this time, oops!]
>
> Here's a patch to add a non-blocking "trylock" method to the  
> omni_mutex
> class in the omnithread library.  Would something like this be a
> useful addition to the omnithread code base?
>
> See that I added the relevant mapping to a native thread library call
> in nt.h, posix.h and VxThread.h.  The last of these is untested.
>
> The other implementations (mach.h, pthread_nt.h and solaris.h) appear
> to be currently broken due to missing a definition of
> OMNI_MUTEX_LOCK_IMPLEMENTATION, so I did not bother with them.
>
> Kind regards,
> Luke.
>
>
> **********************************************************************************************
> Important Note
> This email (including any attachments) contains information which is  
> confidential and may be subject to legal privilege.  If you are not  
> the intended recipient you must not use, distribute or copy this  
> email.  If you have received this email in error please notify the
> sender immediately and delete this email. Any views expressed in  
> this email are not necessarily the views of IRESS Market Technology  
> Limited.
>
> It is the duty of the recipient to virus scan and otherwise test the  
> information provided before loading onto any computer system.
> IRESS Market Technology Limited does not warrant that the  
> information is free of a virus or any other defect or error.
> **********************************************************************************************
> <omni-trylock.patch><mime-attachment.txt>




More information about the omniORB-list mailing list