Re: pthread_join
Re: pthread_join
- Subject: Re: pthread_join
- From: Marc Van Olmen <email@hidden>
- Date: Fri, 10 Sep 2004 17:17:07 -0400
Matt,
I implemented both techniques just to try because the 'attached'
implementation gave some trouble...
In the 'attached' implementation, my virtual memory space keeps on
growing.... after 8 hours it reach 4 GB and my program stops at
'pthread_create' with error: EAGAIN
The number of threads that are there is not growing.
The 'real memory' space is not growing... I used activity monitor to
check this information here above...
The only difference between the semaphore version is the code here
below.
I defined a macro FW_SEMAPHORE_WAIT_TECHNIQUE to switch easy between
the 2 technique's
is there something special I need to do when the Thread's ends in case
of 'attached' thread?
I read the o-reilly book pThread programming and couldn't find anything
but of course i could missed something on this.
Currently in mys code in the the Entry function
I just put it the
inCAPThread->mPThread = 0;
my code is for 95% based on the core-audio public code CAPThread.h and
CAPThread.cpp
regards,
mvo
here the code ( the code is based on my classes i can send all the
related code if necessary, like the semaphore class...) it will be
about 6 files.
/
/-----------------------------------------------------------------------
-----------------
// cOS_Thread::Start
/
/-----------------------------------------------------------------------
-----------------
void cOS_Thread::Start()
{
if(mPThread == 0)
{
OSStatus theResult;
pthread_attr_t theThreadAttributes;
mThreadFinished = false;
theResult = pthread_attr_init(&theThreadAttributes);
OS_ThrowIf(theResult != 0, theResult, "cOS_Thread::Start: Thread
attributes could not be created.");
#if FW_SEMAPHORE_WAIT_TECHNIQUE
theResult = pthread_attr_setdetachstate(&theThreadAttributes,
PTHREAD_CREATE_DETACHED);
OS_ThrowIf(theResult != 0, theResult, "cOS_Thread::Start: A thread
could not be created in the detached state.");
#endif
theResult = pthread_create(&mPThread, &theThreadAttributes,
(ThreadRoutine)cOS_Thread::Entry, this);
OS_ThrowIf(theResult != 0, theResult, "cOS_Thread::Start: Could not
create a thread.");
pthread_attr_destroy(&theThreadAttributes);
if(mTimeConstraintSet)
{
SetTimeConstraints(mPeriod, mComputation, mConstraint,
mIsPreemptible);
}
else
{
SetPriority(mPriority, mFixedPriority);
}
}
}
/
/-----------------------------------------------------------------------
-----------------
// cOS_Thread::Entry
/
/-----------------------------------------------------------------------
-----------------
void* cOS_Thread::Entry(cOS_Thread* inCAPThread)
{
void* theAnswer = NULL;
if(inCAPThread->mThreadRoutine != NULL)
{
#if FW_SEMAPHORE_WAIT_TECHNIQUE
inCAPThread->mWaitSemaphore->Lock();
#endif
theAnswer =
inCAPThread->mThreadRoutine(inCAPThread->mThreadParameter);
inCAPThread->mThreadFinished = true;
#if FW_SEMAPHORE_WAIT_TECHNIQUE
inCAPThread->mWaitSemaphore->Unlock();
inCAPThread->mWaitSemaphore->Notify();
#endif
}
inCAPThread->mPThread = 0;
return theAnswer;
}
/
/-----------------------------------------------------------------------
-----------------
// cOS_Thread::WaitToFinish
/
/-----------------------------------------------------------------------
-----------------
void cOS_Thread::WaitToFinish ( )
{
if (mPThread) {
#if FW_SEMAPHORE_WAIT_TECHNIQUE
mWaitSemaphore->Lock();
while (mThreadFinished == false) {
mWaitSemaphore->Wait();
}
mWaitSemaphore->Unlock();
#else
OS_Error anError = pthread_join(mPThread,NULL);
OS_ThrowIf(anError != 0, anError, "cOS_Thread::WaitToFinish: Could
not wait for a signal");
#endif
FW_ASSERT(mThreadFinished == true);
}
}
On Sep 9, 2004, at 4:10 PM, Matt Watson wrote:
Would this be better (performance wise) then using an attached thread?
You probably wouldn't notice the performance difference, since they
use the same underlying mechanism. You'd do something like this at the
end of your thread's body routine:
pthread_mutex_lock(&mMutex);
gThread_B_Finished = true;
pthread_mutex_unlock(&mMutex);
pthread_cond_signal(&mCondVar);
then in your "waiting" routine, do:
pthread_mutex_lock(& mMutex);
while (gThread_B_Finished == false)
pthread_cond_wait(& mCondVar, & mMutex);
pthread_mutex_unlock(& mMutex);
And you'll want to add error checking appropriate for your application
to the above code, of course.
matt.
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.