[关闭]
@evolxb 2016-06-14T17:07:54.000000Z 字数 4756 阅读 1125

Threading Programming Guide

thread gcd


What Are Threads?

Threading Terminology

Thread State

Entry‐point function

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.

Run Loops

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.

Synchronization Tools

Inter‐thread Communication

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.

Design Tips

Avoid Creating Threads Explicitly

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.

Keep Your Threads Reasonably Busy

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.

Avoid Shared Data Structures

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.

Threads and Your User Interface

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.

Be Aware of Thread Behaviors at Quit Time

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.

Handle Exceptions

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.

Terminate Your Threads Cleanly

The best way for a thread to exit is naturally, by letting it reach the end of its main entry point routine.

Thread Safety in Libraries

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注