@evolxb
2016-06-14T17:07:54.000000Z
字数 4756
阅读 1145
thread
gcd
a thread is a combination of the kernel-level and application-level data structures needed to manage the execution of code.
Having multiple threads in an application provides two very important potential advantages:
thread
is used to refer to a separate path of execution for code.process
is used to refer to a running executable, which can encompass multiple threads.task
is used to refer to the abstract concept of work that needs to be performed.When you create a new thread, you must specify an entry‐point function (or an entry‐point method in the case of Cocoa threads) for that thread.
This entry‐point function constitutes the code you want to run on the thread. When the function returns, or when you terminate the thread explicitly, the thread stops permanently and is reclaimed by the system.
A run loop is a piece of infrastructure used to manage events arriving asynchronously on a thread.
A run loop works by monitoring one or more event sources for the thread. As events arrive, the system wakes up the thread and dispatches the events to the run loop, which then dispatches them to the handlers you specify. If no events are present and ready to be handled, the run loop puts the thread to sleep.
To configure a run loop, all you have to do is launch your thread, get a reference to the run loop object, install your event handlers, and tell the run loop to run.
Locks
provide a brute force form of protection for code that can be executed by only one thread at a time.Conditions
which ensure the proper sequencing of tasks within your application.Atomic operations
offer a lightweight alternative to locks in situations where you can perform mathematical or logical operations on scalar data types.Mechanism | Description |
---|---|
Direct messaging | Cocoa applications support the ability to perform selectors directly on other threads. |
Global variables, shared memory, and objects | Another simple way to communicate information between two threads is to use a global variable, shared object, or shared block of memory. |
Conditions | Conditions are a synchronization tool that you can use to control when a thread executes a particular portion of code. |
Run loop sources | A custom run loop source is one that you set up to receive application‐specific messages on a thread. |
Ports and sockets | Port‐based communication is a more elaborate way to communication between two threads, but it is also a very reliable technique. |
Message queues | The legacy Multiprocessing Services defines a first‐in, first‐out (FIFO) queue abstraction for managing incoming and outgoing data. |
Cocoa distributed objects | Distributed objects is a Cocoa technology that provides a high‐level implementation of port‐based communications. |
Writing thread‐creation code manually is tedious and potentially error‐prone and you should avoid it whenever possible. OS X and iOS provide implicit support for concurrency through other APIs. Rather than create a thread yourself, consider using asynchronous APIs, GCD, or operation objects to do the work.
If you decide to create and manage threads manually, remember that threads consume precious system resources. You should do your best to make sure that any tasks you assign to threads are reasonably long‐lived and productive.
The simplest and easiest way to avoid thread‐related resource conflicts is to give each thread in your program its own copy of whatever data it needs. Parallel code works best when you minimize the communication and resource contention among your threads.
If your application has a graphical user interface, it is recommended that you receive user‐related events and initiate interface updates from your application’s main thread. This approach helps avoid synchronization issues associated with handling user events and drawing window content.
A process runs until all non‐detached threads have exited. By default, only the application’s main thread is created as non‐detached, but you can create other threads that way as well.
Exception handling mechanisms rely on the current call stack to perform any necessary clean up when an exception is thrown. Because each thread has its own call stack, each thread is therefore responsible for catching its own exceptions. Failing to catch an exception in a secondary thread is the same as failing to catch an exception in your main thread: the owning process is terminated. You cannot throw an uncaught exception to a different thread for processing.
The best way for a thread to exit is naturally, by letting it reach the end of its main entry point routine.