views:

227

answers:

4

I'm making an iPhone app using threads. I was used C# for a while, there was a method Thread.Join() which blocks current thread for specific thread completes execution. What's the equivalent in Cocoa for it? Or Alternatives?

---edit---

PS. I'm using NSThread.

---edit---

I'm finding a method like 'waitForThreadExit' which blocks caller thread until thread completes execution.

+1  A: 

If you are using pthreads, then use: pthread_join.

On the other hand, if you are using NSThread class, there is no equivalent to join method you are referring to.

You could try wiht NSObject's message performSelectorOnMainThread:withObject:waitUntilDone:

But I am not exactly sure what you are trying to accomplish here.

Here's is Apple's Multithreading Programming Guide.

Pablo Santa Cruz
Sorry, I'm using NSThread. Is there a way to use the method on NSThread?
Eonil
I dont fully understand your question, but have you had a look at the NSThread documentation? http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSThread_Class/Reference/Reference.html
Niklas Berglund
Yes, but I couldn't find such method like 'waitForThreadExit'. I'll update my question more clear.
Eonil
What you said is just what I was finding, but it's just for pthread, so I don't know how to use it for NSThread.
Eonil
I'll investigate it. Thanks!
Eonil
+1  A: 

The threads created with Cocoa cannot be created as detached. NSThread instances always wrap attached POSIX threads for resource management reasons. As quoted in the Thread Programming Guide:

If you do want to create joinable threads, the only way to do so is using POSIX threads. POSIX creates threads as joinable by default. To mark a thread as detached or joinable, modify the thread attributes using the pthread_attr_setdetachstate function prior to creating the thread. After the thread begins, you can change a joinable thread to a detached thread by calling the pthread_detach function. For more information about these POSIX thread functions, see the pthread man page. For information on how to join with a thread, see the pthread_join man page.

If you are looking for a way to be notified of the end of a NSThread, you can use the NSThreadWillExitNotification notification.

Laurent Etiemble
Thanks for answer. Your information useful! I'm sorry about I couldn't set this as an accepted answer. I should accept also this if I could accept multiple answers.
Eonil
+1  A: 

NSThread does not expose a Join method by any name. NSThread is a very simple, high level, wrapper class. It's very useful for doing threading in a GUI app as it simplifies calling back onto the main thread. For simple backgrounding of tasks and communicating the result back to the main thread on completion this should be sufficient and is fairly easy to get right. If you want to do more "advanced" things (and that includes Join, here) then you'll either have to go to pthreads or layer the semantics on top of NSThread (perhaps by using NSCondition).

Phil Nash
Thanks for the info. NSCondition may will help me a lot!
Eonil
NP, I think NSThreadWillExitNotification may be useful too, as Laurent suggests. Good luck
Phil Nash
A: 

You can do this yourself using NSConditionLock. Define two conditions: "running" and "terminated". The worker thread acquires the lock "running" and upon termination it unlocks with condition "terminated". A join would then be to acquire the lock "terminated" and then unlock it "terminated".

Christian Fries