Multithreading in Cocoa

Multithreading in Cocoa,第1张

概述Cocoa — Multithreading Cocoa is Apple OS X's framework around Objective-C. Objective-C is an object oriented language build on top of C, providing all the pros and pitfalls of ANSI C. Since it runs on Cocoa — Multithreading Cocoa is Apple OS X's framework around Objective-C. Objective-C is an object orIEnted language build on top of C,provIDing all the pros and pitfalls of ANSI C. Since it runs on top of a BSD kernel, pthreads can be used as well (technically,it's a Mach kernel with a BSD-like environment). OS X is a good platform to test multithreaded development because all of their recent systems are multi-core. Not that these are uncommon for other platforms as well,mind you. NSThread and frIEnds There are hundreds of places to find short snippets of Objective-C code. Even examples using NSThread,Cocoa's Thread-spawning object. However,I Could not find a single complete example,so I set out to figure it out myself (along with Objective-C) and document it so other people can save a few minutes. Or something. Anyway,here comes the amazing example:
// main.m // // Created by Christopher Wright on 2007.06.12. #import <Foundation/Foundation.h> @interface MyObject : NSObject +(voID)aMethod:(ID)param; @end @implementation MyObject +(voID)aMethod:(ID)param{ int x; for(x=0;x<50;++x) { printf("Object Thread says x is %i\n",x); usleep(1); } } @end int main(int argc,char *argv[]) { int x; [NSThread detachNewThreadSelector:@selector(aMethod:) toTarget:[MyObject class] withObject:nil]; for(x=0;x<50;++x) { printf("Main thread says x is %i\n",x); usleep(1); } return 0; } 
When you compile from the command line,be sure to use -framework Cocoa,otherwise you're get unresolved symbol errors.

It's important to note that the thread method takes one argument. This is essential. If you leave it out,you'll get error messages whining about Selectors not being found.

This code will start a counter thread,and then run its own counter in main. It's fairly simple,doesn't do any locking,and doesn't pass messages. Because of these deficIEncIEs,it's essentially useless. However,in a few days I hope to expand into the realms of NSLock and frIEnds to show what more can be done.
Using NSLock NSLock is IDentical to a mutex. If you're not sure what a mutex is,head on over to this article which discusses their functionality and usage in multithreaded applications.

An NSLock Object has 4 methods. They're quite simple to use,and their functionality is quite intuitive.

[myLock lock]; locks the lock. This prevents other threads that are trying to acquire the lock from continuing until the lock is released.

[myLock unlock]; unlocks the lock. This can only be done by the thread that locked the lock,and obvIoUsly allows other threads to acquire it.

[myLock tryLock]; attempts to lock the lock,returns NO if it fails,otherwise returns YES.

[myLock lockBeforeDate:]; attempts to lock until a specifIEd date. NO on failure,otherwise YES.

Here's a simple example based on the code above that uses locks.
// main.m // // Created by Christopher Wright on 2007.06.12. #import <Foundation/Foundation.h> NSLock *lock; @interface MyObject : NSObject +(voID)aMethod:(ID)param; @end @implementation MyObject +(voID)aMethod:(ID)param{ int x; for(x=0;x<50;++x) { [lock lock]; printf("Object Thread says x is %i\n",x); usleep(1); [lock unlock]; } } @end int main(int argc,char *argv[]) { int x; lock = [[NSLock alloc] init]; [NSThread detachNewThreadSelector:@selector(aMethod:) toTarget:[MyObject class] withObject:nil]; for(x=0;x<50;++x) { [lock lock]; printf("Main thread says x is %i\n",x); usleep(10000); printf("Main thread lets go\n",x); [lock unlock]; usleep(100); } return 0; } 
In this example,the main thread holds the lock for a relatively long time,and then releases it for a while too. To see that to lock is working,we can check to see if any lines are output between "Main thread says..." and "Main thread lets go". OutsIDe of these pairs it is not uncommon to see a few lines of output from the other thread,since at that point the lock is released,allowing the other thread to acquire the lock and run.

It's important to note that in normal Cocoa apps,you'll need to set up and NSautoreleasePool for each thread. This is done in the thread itself,typically at the beginning of the thread function. If you omit this,you'll get lots of error messages in the Console,and,more importantly,you'll leak memory.

Thread functions can also be Instance methods (-[myClass someThreadedMethod:]),and in that case they're able to access the objects instance variables just as the main thread can -- this is where locking comes in handy,to keep everything consistent.
Using NSRecursiveLock NSRecursiveLock is an object that allows the locking thread to lock it multiplt times. normally if a thread were to lock a lock it already held,it would block forever. Recursive locks get round this,which is handy for recursive functions that obtain locks.

They are used in exactly the same way as NSLock. Still working on that demo.
Leopard updates With MacOS X 10.5 (Leopard),NSThread was enhanced and improved in several ways,making them much easIEr to use and work with. I'll try to whip something up eventually to demonstrate. MacOS X 10.6 added some assistive deBUGging methods,and the ability to set thread priority from other threads (prevIoUsly,only a thread itself Could change its priority). 总结

以上是内存溢出为你收集整理的Multithreading in Cocoa全部内容,希望文章能够帮你解决Multithreading in Cocoa所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1060601.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存