先看一个例子
#include <unistd.h>
#import <Foundation/Foundation.h>
static NSString *ThreadTestDidFinishWork = @"ThreadTestDidFinishWork";
static BOOL gDone = false; // how old fashioned ...
@interface ThreadTest : NSObject
{
}
- (void)startWork;
- (void)doWork:(id)sender;
- (void)finishWorkHandler:(NSNotification*)note;
@end
@implementation ThreadTest
- (void)startWork
{
// register our handler
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(finishWorkHandler:) name:ThreadTestDidFinishWork
object:nil];
// start our thread
[NSThread detachNewThreadSelector:@selector(doWork:) toTarget:self withObject:nil];
}
- (void)doWork:(id)sender
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"work thread: %@", [NSThread currentThread]);
// do time intensive work
sleep(5);
// post notification
[[NSNotificationCenter defaultCenter]
postNotificationName:ThreadTestDidFinishWork object:nil];
[pool release];
}
- (void)finishWorkHandler:(NSNotification*)note
{
NSLog(@"note thread: %@", [NSThread currentThread]);
// unregister
[[NSNotificationCenter defaultCenter] removeObserver:self];
// quit
gDone = true;
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ThreadTest *test = [[ThreadTest alloc] init];
NSLog(@"main thread: %@", [NSThread currentThread]);
[test startWork];
// please don't write real apps like this! this is just for the
// sake of the test! use real run loops! I beg you!
while (!gDone) {
(void)[[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
[test release];
[pool release];
return 0;
}
When the above program is compiled and run it yields the following output:
输出结果:
[localhost:Shared/Builds/mtrent] mike% ./threadNoteTest
Apr 25 00:57:03 threadNoteTest[384] main thread: Thread 0x4c200
Apr 25 00:57:03 threadNoteTest[384] work thread: Thread 0x5f940
Apr 25 00:57:09 threadNoteTest[384] note thread: Thread 0x5f940
So it's clear our notification was posted and received on our worker thread.
以上是线程间在同一进程通信
以下进程间通信:我根据以上程序修改的
接收端:
#import <Cocoa/Cocoa.h>
static NSString *ThreadTestDidFinishWork = @"ThreadTestDidFinishWork";
static BOOL gDone = false; // how old fashioned ...
@interface ThreadTest : NSObject
{
}
- (void)startWork;
- (void)doWork:(id)sender;
- (void)finishWorkHandler:(NSNotification*)note;
@end
@implementation ThreadTest
- (void)startWork
{
// register our handler
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(finishWorkHandler:) name:ThreadTestDidFinishWork
object: @"Pig"];
// start our thread
// [NSThread detachNewThreadSelector:@selector(doWork:) toTarget:self withObject:nil];
}
- (void)doWork:(id)sender
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"work thread: %@", [NSThread currentThread]);
// do time intensive work
sleep(5);
// post notification
// [[NSNotificationCenter defaultCenter]
//postNotificationName:ThreadTestDidFinishWork object:@"test"];
[pool release];
}
- (void)finishWorkHandler:(NSNotification*)note
{
// NSLog(@"note thread: %@", [NSThread currentThread]);
NSLog(@"NSNotificationCenter name: %@", [note name]);
NSLog(@"NSNotificationCenter object: %@", [note object]);
NSLog(@"NSNotificationCenter Info: %@", [[note userInfo] objectForKey: @"test"] );
// unregister
[[NSNotificationCenter defaultCenter] removeObserver:self];
// quit
gDone = true;
}
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ThreadTest *test = [[ThreadTest alloc] init];
NSLog(@"main thread: %@", [NSThread currentThread]);
[test startWork];
// please don't write real apps like this! this is just for the
// sake of the test! use real run loops! I beg you!
// while (!gDone) {
// (void)[[NSRunLoop currentRunLoop]
//runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
// }
// [test release];
[pool release];
// return 0;
return NSApplicationMain(argc, (const char **) argv);
}
调用段:
#include <unistd.h>
#import <Foundation/Foundation.h>
static NSString *ThreadTestDidFinishWork = @"ThreadTestDidFinishWork";
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"main thread: %@", [NSThread currentThread]);
NSMutableDictionary* info = [NSMutableDictionary dictionary];
[info setObject: @"sdfsdfdsgsdg" forKey: @"test"];
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:ThreadTestDidFinishWork object: @"Pig" userInfo: info deliverImmediately: YES];
[info release];
[pool release];
return 0;
}
本人经验有限,希望大家多提意见