mutex for c++

From: Bill Chiu (billchiu_at_despammed.com)
Date: 10/31/03


Date: 31 Oct 2003 01:01:37 -0800

Dear C++ Universe,

I wrap pthread lock with a C++ class, and it caused crashes.. Is the
below implementation a real baddy and needs serious spanking??

#include <pthread.h>
class Lock {
 public:
  Lock() { pthread_mutex_init(&plock, NULL); }
  ~Lock() {}
  void lock() { pthread_mutex_lock(&plock); }
  void unlock() { pthread_mutex_unlock(&plock); }

 private:
  pthread_mutex_t plock;
};

class testdriver {
  testdriver();
 private:
  Lock testlock;
}
testdriver::testdriver() {
}

p.s. I don't want to use boost lib either.



Relevant Pages

  • Re: thread-safety
    ... and I started to use dotnet's queue collection with lock protection. ... private enum WorkerThreadState; ... private object stateLocker = new object; ... public void Start ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: thread-safety
    ... It supports immediate addition to the consumer's queue, and creation of a repeating timer that will periodicially add to the consumer's queue. ... private TimeSpan _tsExecute; ... // the JobConsumer class while holding it's own lock. ... public void Add ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: .net 2.0 : looking for a "best practice" for multi threading jobs
    ... private int m_numberOfThreads; ... private object m_numberOfThreadsLockObject = new object; ... lock ... public void AddAction ...
    (microsoft.public.dotnet.framework)
  • Re: SyncLock documentation
    ... You should declare a Private object variable to ... variable to protect data common to all instances. ... You should not use the Me keyword to provide a lock object for instance ... In the example below, bbb uses 'synclock me', and ccc uses 'synclock sss' ...
    (microsoft.public.dotnet.languages.vb)
  • Re: lock statement question
    ... I have a private timer member whose interval can be changed by different threads. ... When you lock on an object, it doesn't protect the object itself from access in any way, it only prevents other threads to enter any code that is locked using the same object. ... They use the same object for locking, so while some thread is using one of the methods, no other thread can use any of the two methods. ...
    (microsoft.public.dotnet.languages.csharp)