public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* critical section
@ 2001-07-31 14:31 Ye Liu
  2001-07-31 15:22 ` Scott McCaskill
  0 siblings, 1 reply; 10+ messages in thread
From: Ye Liu @ 2001-07-31 14:31 UTC (permalink / raw)
  To: win32-pthread

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2001-08-07 20:52 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-31 14:31 critical section Ye Liu
2001-07-31 15:22 ` Scott McCaskill
2001-07-31 15:39   ` Ye Liu
2001-07-31 15:52     ` Scott McCaskill
2001-08-05 21:31       ` catch (...) Ye Liu
2001-08-05 22:07         ` Scott McCaskill
2001-08-05 22:12           ` Ye Liu
2001-08-06 20:38           ` Ross Johnson
2001-08-07  8:49             ` reentrant
2001-08-07 20:52               ` Ross Johnson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).