Threading_Programming_Guide_2014_Apple.pdf

(895 KB) Pobierz
Threading Programming
Guide
Contents
Introduction
8
Organization of This Document
8
See Also
9
About Threaded Programming
10
What Are Threads?
10
Threading Terminology
11
Alternatives to Threads
12
Threading Support
13
Threading Packages
14
Run Loops
15
Synchronization Tools
15
Inter-thread Communication
16
Design Tips
17
Avoid Creating Threads Explicitly
18
Keep Your Threads Reasonably Busy
18
Avoid Shared Data Structures
18
Threads and Your User Interface
19
Be Aware of Thread Behaviors at Quit Time
19
Handle Exceptions
20
Terminate Your Threads Cleanly
20
Thread Safety in Libraries
20
Thread Management
22
Thread Costs
22
Creating a Thread
24
Using NSThread
24
Using POSIX Threads
25
Using NSObject to Spawn a Thread
27
Using POSIX Threads in a Cocoa Application
28
Configuring Thread Attributes
28
Configuring the Stack Size of a Thread
29
Configuring Thread-Local Storage
29
Setting the Detached State of a Thread
30
2014-07-15 | Copyright © 2014 Apple Inc. All Rights Reserved.
2
Contents
Setting the Thread Priority
30
Writing Your Thread Entry Routine
31
Creating an Autorelease Pool
31
Setting Up an Exception Handler
32
Setting Up a Run Loop
32
Terminating a Thread
33
Run Loops
35
Anatomy of a Run Loop
35
Run Loop Modes
36
Input Sources
38
Timer Sources
40
Run Loop Observers
41
The Run Loop Sequence of Events
41
When Would You Use a Run Loop?
42
Using Run Loop Objects
43
Getting a Run Loop Object
43
Configuring the Run Loop
44
Starting the Run Loop
45
Exiting the Run Loop
47
Thread Safety and Run Loop Objects
47
Configuring Run Loop Sources
48
Defining a Custom Input Source
48
Configuring Timer Sources
55
Configuring a Port-Based Input Source
56
Synchronization
67
Synchronization Tools
67
Atomic Operations
67
Memory Barriers and Volatile Variables
68
Locks
68
Conditions
70
Perform Selector Routines
70
Synchronization Costs and Performance
71
Thread Safety and Signals
72
Tips for Thread-Safe Designs
72
Avoid Synchronization Altogether
73
Understand the Limits of Synchronization
73
Be Aware of Threats to Code Correctness
73
Watch Out for Deadlocks and Livelocks
75
2014-07-15 | Copyright © 2014 Apple Inc. All Rights Reserved.
3
Contents
Use Volatile Variables Correctly
75
Using Atomic Operations
76
Using Locks
79
Using a POSIX Mutex Lock
79
Using the NSLock Class
80
Using the @synchronized Directive
81
Using Other Cocoa Locks
81
Using Conditions
85
Using the NSCondition Class
85
Using POSIX Conditions
86
Thread Safety Summary
88
Cocoa
88
Foundation Framework Thread Safety
88
Application Kit Framework Thread Safety
94
Core Data Framework
96
Core Foundation
96
Glossary
97
Document Revision History
99
Swift
7
2014-07-15 | Copyright © 2014 Apple Inc. All Rights Reserved.
4
Figures, Tables, and Listings
About Threaded Programming
10
Table 1-1
Table 1-2
Table 1-3
Alternative technologies to threads
12
Thread technologies
14
Communication mechanisms
16
Thread Management
22
Table 2-1
Table 2-2
Listing 2-1
Listing 2-2
Listing 2-3
Thread creation costs
23
Setting the stack size of a thread
29
Creating a thread in C
26
Defining your thread entry point routine
31
Checking for an exit condition during a long job
33
Run Loops
35
Figure 3-1
Figure 3-2
Table 3-1
Table 3-2
Listing 3-1
Listing 3-2
Listing 3-3
Listing 3-4
Listing 3-5
Listing 3-6
Listing 3-7
Listing 3-8
Listing 3-9
Listing 3-10
Listing 3-11
Listing 3-12
Listing 3-13
Listing 3-14
Listing 3-15
Listing 3-16
Listing 3-17
Structure of a run loop and its sources
36
Operating a custom input source
49
Predefined run loop modes
37
Performing selectors on other threads
39
Creating a run loop observer
44
Running a run loop
46
The custom input source object definition
49
Scheduling a run loop source
51
Performing work in the input source
51
Invalidating an input source
52
Installing the run loop source
52
Registering and removing an input source with the application delegate
53
Waking up the run loop
54
Creating and scheduling timers using NSTimer
55
Creating and scheduling a timer using Core Foundation
56
Main thread launch method
57
Handling Mach port messages
57
Launching the worker thread using Mach ports
58
Sending the check-in message using Mach ports
59
Registering a message port
60
Attaching a Core Foundation message port to a new thread
61
2014-07-15 | Copyright © 2014 Apple Inc. All Rights Reserved.
5
Zgłoś jeśli naruszono regulamin