Using POSIX Threads in a Cocoa Application
2011-07-18 18:07
176 查看
Using POSIX Threads in a Cocoa Application
Although the NSThread class is the main interface for creating threads in Cocoa applications, you are free to use POSIX threads instead if doing so is more convenient for you. For example, you might use POSIX threads if you already have code that uses them and you do not want to rewrite it. If you do plan to use the POSIX threads in a Cocoa application, you should still be aware of the interactions between Cocoa and threads and obey the guidelines in the following sections.
Protecting the Cocoa Frameworks
For multithreaded applications, Cocoa frameworks use locks and other forms of internal synchronization to ensure they behave correctly. To prevent these locks from degrading performance in the single-threaded case, however, Cocoa does not create them until the application spawns its first new thread using the NSThread class. If you spawn threads using only POSIX thread routines, Cocoa does not receive the notifications it needs to know that your application is now multithreaded. When that happens, operations involving the Cocoa frameworks may destabilize or crash your application.
To let Cocoa know that you intend to use multiple threads, all you have to do is spawn a single thread using the NSThread class and let that thread immediately exit. Your thread entry point need not do anything. Just the act of spawning a thread using NSThread is enough to ensure that the locks needed by the Cocoa frameworks are put in place.
If you are not sure if Cocoa thinks your application is multithreaded or not, you can use the isMultiThreaded method of NSThread to check.
Mixing POSIX and Cocoa Locks
It is safe to use a mixture of POSIX and Cocoa locks inside the same application. Cocoa lock and condition objects are essentially just wrappers for POSIX mutexes and conditions. For a given lock, however, you must always use the same interface to create and manipulate that lock. In other words, you cannot use a Cocoa NSLock object to manipulate a mutex you created using the pthread_mutex_init function, and vice versa.
Although the NSThread class is the main interface for creating threads in Cocoa applications, you are free to use POSIX threads instead if doing so is more convenient for you. For example, you might use POSIX threads if you already have code that uses them and you do not want to rewrite it. If you do plan to use the POSIX threads in a Cocoa application, you should still be aware of the interactions between Cocoa and threads and obey the guidelines in the following sections.
Protecting the Cocoa Frameworks
For multithreaded applications, Cocoa frameworks use locks and other forms of internal synchronization to ensure they behave correctly. To prevent these locks from degrading performance in the single-threaded case, however, Cocoa does not create them until the application spawns its first new thread using the NSThread class. If you spawn threads using only POSIX thread routines, Cocoa does not receive the notifications it needs to know that your application is now multithreaded. When that happens, operations involving the Cocoa frameworks may destabilize or crash your application.
To let Cocoa know that you intend to use multiple threads, all you have to do is spawn a single thread using the NSThread class and let that thread immediately exit. Your thread entry point need not do anything. Just the act of spawning a thread using NSThread is enough to ensure that the locks needed by the Cocoa frameworks are put in place.
If you are not sure if Cocoa thinks your application is multithreaded or not, you can use the isMultiThreaded method of NSThread to check.
Mixing POSIX and Cocoa Locks
It is safe to use a mixture of POSIX and Cocoa locks inside the same application. Cocoa lock and condition objects are essentially just wrappers for POSIX mutexes and conditions. For a given lock, however, you must always use the same interface to create and manipulate that lock. In other words, you cannot use a Cocoa NSLock object to manipulate a mutex you created using the pthread_mutex_init function, and vice versa.
相关文章推荐
- QT8&9&10 Login Form using sqlite in QT application
- Using Spring.net in console application
- Using the Enterprise Library Validation Application Block in ASP.NET - Part I
- create Context Menu in Windows Forms application using C# z
- Using Spring.net in console application
- Using the Enterprise Library Validation Application Block in ASP.NET - Part II
- Restart your Cocoa application using Sparkle
- Using button controls in an application
- Synchronizing Threads and GUI in Delphi application
- Using Java Classes in your .NET Application -摘自网络
- Using Couchbase Server In A GoLang Web Application
- Creating custom headers and footers in Application level events using global.asax
- Using GET_APPLICATION_PROPERTY in Oracle D2k Forms
- Using Theme in an Application--在应用中使用主题(Symbian)
- 2010年7月19日_周一_Using the Common Data Source API in a Web application
- Debugging Memory Related Issues in .Net Application Using WinDBG and SOS
- Integrating OpenID in an ASP.NET MVC Application using DotNetOpenAuth
- [转载]Step By Step - Using Silverlight to Access a WCF Service Hosted In a Console Application
- Using a Console in Winform Application
- Persisting iOS Application Data in SQLite Database Using FMDB