From: "Ye Liu" <yliu@tibco.com>
To: win32-pthread <pthreads-win32@sourceware.cygnus.com>
Subject: critical section
Date: Tue, 31 Jul 2001 14:31:00 -0000 [thread overview]
Message-ID: <3B6722DF.475EAC12@tibco.com> (raw)
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
next reply other threads:[~2001-07-31 14:31 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-07-31 14:31 Ye Liu [this message]
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
2001-07-31 16:08 critical section Eli Ofenstein
2001-07-31 20:24 ` Ye Liu
2001-07-31 22:47 ` Ross Johnson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3B6722DF.475EAC12@tibco.com \
--to=yliu@tibco.com \
--cc=pthreads-win32@sourceware.cygnus.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).