473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

mutex for c++

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.
Jul 19 '05 #1
7 20614
"Bill Chiu" <bi******@despammed.com> wrote...
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() {}
I've always wondered, shouldn't such lock be released upon destruction?
void lock() { pthread_mutex_lock(&plock); }
void unlock() { pthread_mutex_unlock(&plock); }

private:
pthread_mutex_t plock;
};

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

And what this testdriver supposed to accomplish? It cannot be
instantiated (private constructor), neither does it lock or
unlock its testlock...
testdriver::testdriver() {
}

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


Maybe you should. At least they debug it.

Victor
Jul 19 '05 #2
Hey,

Yeah I guess I forgot to release the lock on destructor and public the
constructor on that last one.. is this new code below good
wrapper/impl of lock? I worry extra code added by c++ (if any) is not
thread safe... comments? :)

#include <pthread.h>

class Lock {
public:
Lock() { pthread_mutex_init(&plock, NULL); }
~Lock() { pthread_mutex_unlock(&plock); }

void lock() { pthread_mutex_lock(&plock); }
void unlock() { pthread_mutex_unlock(&plock); }

private:
pthread_mutex_t plock;
};

class testdriver {
public:
testdriver();
private:
Lock testlock; // create a lock
}

Bill
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<RMuob.69414$HS4.623219@attbi_s01>...
"Bill Chiu" <bi******@despammed.com> wrote...
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() {}


I've always wondered, shouldn't such lock be released upon destruction?
void lock() { pthread_mutex_lock(&plock); }
void unlock() { pthread_mutex_unlock(&plock); }

private:
pthread_mutex_t plock;
};

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

;

And what this testdriver supposed to accomplish? It cannot be
instantiated (private constructor), neither does it lock or
unlock its testlock...
testdriver::testdriver() {
}

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


Maybe you should. At least they debug it.

Victor

Jul 19 '05 #3
> Yeah I guess I forgot to release the lock on destructor and public the
constructor on that last one.. is this new code below good
wrapper/impl of lock?


no.

This is:

http://appcore.home.comcast.net/pthreads/mutex.h

Go to the mutex.h link.

--
The designer of the experimental, SMP and HyperThread friendly, AppCore
library.

http://AppCore.home.comcast.net
Jul 19 '05 #4

SenderX wrote:
Yeah I guess I forgot to release the lock on destructor and public the
constructor on that last one.. is this new code below good
wrapper/impl of lock?


no.

This is:

http://appcore.home.comcast.net/pthreads/mutex.h

Go to the mutex.h link.


This was just an illustration. It doesn't properly report resource-not-
available errors. Mutex constructor can throw std::bad_alloc, to begin
with (std::try_again and std::no_way aside for a moment ;-) ). As for
the lock acquisition... since lock initialization can be done in "a
lazy fashion", the same applies for the initial calls done by threads;
"in general", so to speak. Here is an illustration to clarify it.

#define SWAP_BASED_MUTEX_FOR_WINDOWS_INITIALIZER { 0, 0 }

class swap_based_mutex_for_windows {

/* ... */

// -1: free, 0: locked, 1: contention
atomic<int> m_lock_status;
atomic<auto_reset_event *> m_retry_event;

/* ... */

void lock() {
if (m_lock_status.swap(0, msync::acq) >= 0)
slow_lock();
}

bool trylock() {
return m_lock_status.swap(0, msync::acq) >= 0 ?
slow_trylock() : true;
}

bool timedlock(absolute_timeout const & timeout) {
return m_lock_status.swap(0, msync::acq) >= 0 ?
slow_timedlock(timeout) : true;
}

void unlock() {
if (m_lock_status.swap(-1, msync::rel) > 0)
m_retry_event.load(msync::none)->set();
}

void slow_lock() {
auto_reset_event & retry_event = DCSI();
while (m_lock_status.swap(1, msync::acq) >= 0)
retry_event.wait();
}

bool slow_trylock() {
DCSI();
return m_lock_status.swap(1, msync::acq) < 0;
}

bool slow_timedlock(absolute_timeout const & timeout) {
auto_reset_event & retry_event = DCSI();
while (m_lock_status.swap(1, msync::acq) >= 0)
if (!retry_event.timedwait(timeout)) return false;
return true;
}

auto_reset_event & DCSI() {
auto_reset_event * retry_event;
if ((retry_event = m_retry_event.load(msync::none)) == 0) {
named_windows_mutex_trick guard(this);
if ((retry_event = m_retry_event.load(msync::none)) == 0) {
retry_event = new auto_reset_event()
m_retry_event.store(retry_event, msync::rel);
m_lock_status.store(-1, msync::rel);
}
}
return *retry_event;
}

};

regards,
alexander.

--
typedef std::aligned_storage<std::mutex> pthread_mutex_t; // POD
extern "C" int pthread_mutex_destroy(pthread_mutex_t * m) throw() {
m->object().~mutex();
return 0;
}
Jul 19 '05 #5
I suggest to read the following documents to correct your locking
class.

1. Boost.Threads - Mutex Concept
http://boost.org/libs/thread/doc/mutex_concept.html

2. Resource acquisition is initialization
http://c2.com/cgi/wiki?ResourceAcqui...Initialization

3. Scoped Locking
Synchronized Block, Guard, Execute-Around Object
http://www.dpunkt.de/leseproben/3-89...ng-Pattern.pdf
(German)
übersetztes Buch "Pattern-orientierte Software-Architektur: Muster
für nebenläufige und vernetzte Objekte", ISBN 3-89864-142-2
original book "Pattern-Oriented Software Architecture. Volume 2:
Patterns for Concurrent and Networked Objects", John Wiley & Sons Ltd.

4. Double-Checked Locking
http://patterndigest.com/patterns/Do...edLocking.html
Jul 19 '05 #6
> I wrap pthread lock with a C++ class, and it caused crashes... Is the
below implementation a real baddy and needs serious spanking?? .... p.s. I don't want to use boost lib either.


You can avoid to repeat some design mistakes if you choose and use one
of the existing class libraries. Please look at discussions on the
topic "C++ threads".
http://groups.google.de/groups?th=80...amming.threads

If you do not like the Boost library, try these implementations:
- http://pmade.org/software/safept/dow...x_1_1Lock.html
- http://zthread.sourceforge.net/html/..._1_1Mutex.html
Jul 19 '05 #7
one more library:
MutexLock Class Reference
http://www.gnu.org/software/commoncp...utex_lock.html
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
5
by: Ken Varn | last post by:
I have a named mutex object that is accessed by both an asp.net application and a Windows executable .net application. The Windows executable runs under the administrator logon, while the asp.net...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
4
by: TGOS | last post by:
I was thinking about it for a while, a mutex written in C and without disabling any interrupts. Is it possible? typdef struct mutex { unsigned int owner1; unsigend int owner2; } *mutex; ...
2
by: Martin Maat | last post by:
Hi. I want to use the same mutex in different classes (web pages in an ASP.NET application). In global.asax.cs, the class that starts up first, I create a Mutex like this: static private...
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
2
by: tony.newsgrps | last post by:
Hi there, I'm trying to understand the impact of killing a process that owns a system mutex (used to ensure there is only 1 instance of my program running) Here is my code pretty much: try...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
11
by: Lamont Sanford | last post by:
Given an object of type Mutex, what method or property should be called to determine if it is currently owned? I could call WaitOne(0,false) -- and if the return value is false, I could deduce...
2
by: tshad | last post by:
I am running a program as a Windows service which works fine. I am using a Mutex to prevent multiple threads from from accessing my log text file at the same time. It works fine in the Service:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.