Concurrency


관련 아티클



Concurrency API



Threading Programming

✓ Threads are subunits of processes, which can be scheduled independently by the operating system scheduler.

✓ Virtually all concurrency APIs are built on top of threads under the hood – that’s true for both Grand Central Dispatch and operation queues.

✓ The term thread is used to refer to a separate path of execution for code.

✓ The term process is used to refer to a running executable, which can encompass multiple threads.

✓ The term task is used to refer to the abstract concept of work that needs to be performed.

✓ From a technical standpoint, a thread is a combination of the kernel-level and application-level data structures needed to manage the execution of code.

✓ The kernel-level structures coordinate the dispatching of events to the thread and the preemptive scheduling of the thread on one of the available cores.

✓ The application-level structures include the call stack for storing function calls and the structures the application needs to manage and manipulate the thread’s attributes and state.

✓ After starting a thread, the thread runs in one of three main states: running, ready, or blocked. If a thread is not currently running, it is either blocked and waiting for input or it is ready to run but not scheduled to do so yet. The thread continues moving back and forth among these states until it finally exits and moves to the terminated state.

✓ When you create a new thread, you must specify an entry-point function. Because threads are relatively expensive to create in terms of memory and time, it is therefore recommended that your entry point function do a significant amount of work or set up a run loop to allow for recurring work to be performed.

✓ Because threads in a single application share the same memory space, they have access to all of the same data structures. If two threads try to manipulate the same data structure at the same time, one thread might overwrite another’s changes in a way that corrupts the resulting data structure.

✓ Multiple threads can be executed at the same time on a single CPU core (or at least perceived as at the same time). The operating system assigns small slices of computing time to each thread.

Cost

✓ Threads don’t come for free. Each thread ties up memory and kernel resources. ✓ The minimum allowed stack size for secondary threads is 16 KB and the stack size must be a multiple of 4 KB. The space for this memory is set aside in your process space at thread creation time, but the actual pages associated with that memory are not created until they are needed.



Run Loops

✓ A run loop is a piece of infrastructure used to manage events arriving asynchronously on a 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.

✓ Because a run loop puts its thread to sleep when there is nothing to do, it eliminates the need for polling, which wastes CPU cycles and prevents the processor itself from sleeping and saving power.

🙉 Polling 방식?

✓ 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. The infrastructure provided by OS X handles the configuration of the main thread’s run loop for you automatically. If you plan to create long-lived secondary threads, however, you must configure the run loop for those threads yourself.

RunLoop Mode

✓ A run loop mode is a collection of input sources and timers to be monitored and a collection of run loop observers to be notified. Each time you run your run loop, you specify (either explicitly or implicitly) a particular “mode” in which to run. During that pass of the run loop, only sources associated with that mode are monitored and allowed to deliver their events.

스크롤 될 떄의 런루프 모드 (타이머 관련 이슈)

✓ A typical example of this is scrolling on iOS. While you’re scrolling, the run loop is not running in its default mode, and therefore, it’s not going to react to, for example, a timer you have scheduled before. Once scrolling stops, the run loop returns to the default mode and the events which have been queued up are executed. If you want a timer to fire during scrolling, you need to add it to the run loop in the NSRunLoopCommonModes mode.



Queue-based concurrency APIs

GCD

✓ GCD decides on which particular thread your code blocks are going to be executed on, and it manages these threads according to the available system resources.

✓ Think about work items in a queue rather than threads.


Operation Queues

✓ The NSOperationQueue class has two different types of queues: the main queue and custom queues. The main queue runs on the main thread, and custom queues are processed in the background. In any case, the tasks which are processed by these queues are represented as subclasses of NSOperation.

maxConcurrentOperationCount property. Setting it to one gives you a serial queue, which is great for isolation purposes.



😫 여러가지 좋지 않은 상황들과 솔루션들

이유: Sharing of Resources

Race Conditions

Lock

language level support (atomic)

Dead Lock

Explosion