Re: Using POSIX threads with Cocoa?
Re: Using POSIX threads with Cocoa?
- Subject: Re: Using POSIX threads with Cocoa?
- From: Dave Camp <email@hidden>
- Date: Wed, 21 Apr 2004 10:40:58 -0700
On Apr 21, 2004, at 9:17 AM, Vince Ackerman wrote:
Thanks. I have searched everywhere for documentation on pthreads and
can't find much definitive info... perhaps you could give me some
basic instruction in pthreads/mutex? I'm not sure what the following
are doing (if I could understand it, maybe I could write the same in
Cocoa):
oreilly publishes a good book on the subject. I learned my pthread
skills from this book.
<
http://www.oreilly.com/catalog/pthread/index.html>
// In TeleoEasy_Start, what are they initializing here? Maybe if I
understood the "big picture" of what mutex_init does it would answer a
lot of the other questions.
pthread_mutex_init( &teleoThreadMutex, NULL );
This initializes the mutex structure to a known state. The cocoa
equivalent would be an NSLock.
// What is the difference between "pthread_mutex_init" and
"pthread_create" and "pthread_join"?
pthread_create creates and starts the thread. The cocoa equivalent
would be NSThread.
pthread_join causes the current thread to block until the passed thread
exits.
void TeleoEasy_Continue() // Waits for the library to exit
{
pthread_join( teleoInputThread, NULL );
pthread_join( teleoTimerThread, NULL );
pthread_mutex_destroy( &teleoThreadMutex );
}
That code waits for the two other threads to terminate and destroys the
mutex. It's the sort of thing you would do at app cleanup time,
assuming your threads will stop. The code you posted previously had
threads that would loop forever, so the above code would block the main
thread forever.
Another question, if I detach a my own thread (the doinput thread, for
example) with NSThread, will the TeleEasy_Lock / Unlock calls within
that thread still work? Are the Lock/Unlock funtions acting on the
thread they are in, or locking the other threads? Dumb question I'm
sure, but as I mentioned, I don't understand threading as well as I'd
like to... As far as I can tell, the doinput thread reads characters
(get/set calls from the main thread) and passes them to the network
manager?
Sounds like you need to get a good book on threads.
They have a great web site with good documentation online.... but some
of it is still over my head. If it would help, here's the main page
[snip]
Thanks for your help! Sorry if this is taking up bandwidth......
I think once you study up on threading (pthreads in particular so you
relate to their code) much of what they documented will start to make
sense. At that point, you could probably transition to using NSThreads
and such if you wanted.
Dave
---
The path of least resistance, it's not just for electricity any more.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.