Boost logo

Boost Users :

Subject: Re: [Boost-users] [Locks/Mutex] choices between types
From: Markus Bernhardt (markus.bernhardt_at_[hidden])
Date: 2009-07-01 13:45:10


Hi Alex,

A mutex object contains all the data structures for locking.

A lock object locks the given mutex object in the constructor and
unlocks it in its destructor.

Example:

class ThreadSafeSomething
{
 private:
  boost::mutex mutex;
 public:
  void doSomething()
  {
    boost::mutex::scoped_lock(mutex);
    something();
  } // mutex is unlocked here
};

If there would be no lock object you would have to write something like (welcome to java world):

class ThreadSafeSomething
{
 private:
  my::special::mutex mutex;
 public:
  void doSomething()
  {
    mutex.lock();
    something();
    mutex.unlock();
  }
};

Final question: What happens when an exception is thrown in something()?

Kind regards,
Markus

Am Mittwoch, den 01.07.2009, 17:08 +0200 schrieb Axel:
> Hello
>
> This post may not be well suited to this mailing list.
> I have some questions about the lockable concept provided in boost.
> There are many "lock" types, and many "mutex" types.
> My main question is then : when should I chose a given lock type rather
> a given mutex type ?
> Given the fact that some of lock types are not thread-safe, if only one
> thread needs locks, why should I use one of these lock types and why
> shouldnt I use some mutex type ?
>
> Finally, what were the goals of providing so much different ways to
> lock/unlock resources ? (I may not be aware of all the concepts related
> to secure resource access)
>
> Since the "lock" et "mutex" types provide ways to lock/unlock access to
> resources, why are not these types defined under the same name ? Are the
> concepts they implements much more different than I think ?
>
>
> Thanks
> Axel
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net