Sep 11, 2008

(Objective-C) How to block thread

In the multi-threading programming, sometimes we need block the other threads to wait the result of a thread. There are several solutions:

1. Using @synchronized. With this solution, we don't create a NSThread object, here is an example:

- (void) callThreadMethod {
@synchronized(self) {
//the methods you want the thread to do
}
}

2. Using NSLock. Still don't create NSThread object, here is an example:

- (void) callThreadMethod {
NSLock *myLock = [[NSLock alloc] init];
[myLock lock];
//the methods you want the thread to do
[myLock unlock];
}

For the other solutions, you could read this great tutorial of multi-threading, Multithreading Tutorial

No comments: