This is the mail archive of the
pthreads-win32@sources.redhat.com
mailing list for the pthreas-win32 project.
Re: critical section
- To: "Ye Liu" <yliu at tibco dot com>,<pthreads-win32 at sourceware dot cygnus dot com>
- Subject: Re: critical section
- From: "Scott McCaskill" <scott at magruder dot org>
- Date: Tue, 31 Jul 2001 17:22:45 -0500
- References: <3B6722DF.475EAC12@tibco.com>
It looks like you're trying to make a non-recursive mutex (can only be
locked once by any thread, including the mutex owner). pthread_mutex can
already do this; is there some reason you're not using it this way directly?
See pthread_mutexattr_settype().
----- Original Message -----
From: "Ye Liu" <yliu@tibco.com>
To: "win32-pthread" <pthreads-win32@sourceware.cygnus.com>
Sent: Tuesday, July 31, 2001 4:27 PM
Subject: critical section
> Greets,
>
> When I impelement the critical section using mutex, I have the following
> code:
>
> /* TIBMutex.h */
> #ifndef _TIBMutex_H_
> #define _TIBMutex_H_
> class TIBMutex
> {
> public:
> TIBMutex();
> ~TIBMutex();
> void acquire();
> void acquire_yield();
> void release();
> int tryacquire();
>
> private:
> pthread_mutex_t m_Mutex;
> pthread_mutexattr_t m_MutexAttr;
> void init_MutexAttr();
> int m_Count;
> };
>
> /* TIBMutex.cpp */
> TIBMutex::TIBMutex()
> {
> m_Count = 0;
> if (pthread_mutex_init(&m_Mutex, &m_MutexAttr))
> /* error handling...*/
> }
>
> inline void TIBMutex::acquire()
> {
> while (m_Count);
>
> if (pthread_mutex_lock(&m_Mutex))
> {
> /* error handling...*/
> }
> ++m_Count;
> return;
> }
>
> inline void TIBMutex::release()
> {
> if (pthread_mutex_unlock(&m_Mutex))
> {
> /* error handling...*/
> }
> --m_Count;
> }
>
> My questions are:
>
> 1. Do I need use a condition variable here because I use m_Count as 0/1
> signal?
>
> 2. In the acquire(), when m_Count is not 0, the waiting thread should
> "spin" there as the above or yield like the following acquire_yield()
>
> inline void TIBMutex::acquire_yield()
> {
> while (m_Count)
> shed_yield();
> if (pthread_mutex_lock(&m_Mutex))
> {
> /* error handling...*/
> }
> ++m_Count;
> return;
> }
>
>
> --
> Ye Liu
> Tel(O) 650-846-5228
>
>
>