Thread_Management

an overview of the thread technologies available in OS X and iOS along with examples of how to use those technologies in your applications

Posted by kunnan on May 6, 2018

前言

基本概念

  • 进程: 一个具有一定独立功能的程序关于某个数据集合的一次运行活动。可以理解成一个运行中的应用程序。
  • 线程: 程序执行流的最小单元,线程是进程中的一个实体。
  • 同步: 只能在当前线程按先后顺序依次执行,不开启新线程。
  • 异步: 可以在当前线程开启多个新线程执行,可不按顺序执行。
  • 队列: 装载线程任务的队形结构。
  • 并发: 线程执行可以同时一起进行执行。
  • 串行: 线程执行只能依次逐一先后有序的执行。

I、 多线程

  • Your application does not need to create these objects explicitly;
    1)each thread, including the application’s main thread, has an associated run loop object. 
    2)Only secondary threads need to run their run loop explicitly, however. The app frameworks automatically set up and run the run loop on the main thread as part of the application startup proces
    

1、1 pthread_t

  • pthread_t 的定义和演示

1、2 NSThread

一个NSThread对象就代表一条线程

  • 线程的状态

1、3 GCD(Grand Central Dispatch)

  • 纯C语言,提供了非常多强大的函数。GCD是苹果公司为多核的并行运算提出的解决方案
    GCD会自动利用更多的CPU内核(比如双核、四核)
    GCD会自动管理线程的生命周期(创建线程、调度任务、销毁线程)
    程序员只需要告诉GCD想要执行什么任务,不需要编写任何线程管理代码
    
1、3、1 任务和队列
  • GCD中有2个核心概念
    任务:执行什么操作
    队列:用来存放任务
    
  • GCD的使用就2个步骤
    1)定制任务:确定想做的事情
    2) 将任务添加到队列中: 
    GCD会自动将队列中的任务取出,放到对应的线程中执行
    任务的取出遵循队列的FIFO原则:先进先出,后进后出
    
  • 执行任务

1.4 NSOperation

  • 简介
1.4.0 NSOperation的子类
  • NSOperation的子类

  • 1) NSInvocationOperation *
  • 2) NSBlockOperation
  • 3)自定义子类继承NSOperation,实现内部相应
  • NSOperationQueue
1.4.1 NSOperation的方法
  • 操作优先级
  • 操作的监听
  • 操作依赖
1.4.2 cell下载图片思路 – 用沙盒缓存(使用md5生成图片名称,保证唯一;或者使用url)

II、Thread Costs

  • The core structures needed to manage your thread and coordinate its scheduling are stored in the kernel using wired memory.
    Your thread’s stack space and per-thread data is stored in your program’s memory space. 
    Most of these structures are created and initialized when you first create the thread—a process that can be relatively expensive because of the required interactions with the kernel.
    

2.1 Thread creation costs

  • Kernel data structures
    Approximately 1 KB
    
  • Stack space
    512 KB (secondary threads)
    8 MB (OS X main thread)
    1 MB (iOS main thread)
    
  • Creation time
    Approximately 90 microseconds
    
  • see Concurrency Programming Guide.

III、Creating a Thread

3.1 Using NSThread

  • There are two ways to create a thread using the NSThread class:

3.2 Using POSIX Threads

  • OS X and iOS provide C-based support for creating threads using the POSIX thread API
    This technology can actually be used in any type of application (including Cocoa and Cocoa Touch applications) and might be more convenient if you are writing your software for multiple platforms
    
  • Creating a thread in C

IV、Using NSObject to Spawn a Thread

  • Using NSObject to Spawn a Thread
    [myObj performSelectorInBackground:@selector(doSomething) withObject:nil];
    

V、Writing Your Thread Entry Routine

5.1 Creating an Autorelease Pool

  • Defining your thread entry point routine

VI、 Terminating a Thread

  • Checking for an exit condition during a long job

See Also

/Users/devzkn/bin/knpost Thread_Management an overview of the thread technologies available in OS X and iOS along with examples of how to use those technologies in your applications -t Thread
#原来""的参数,需要自己加上""

转载请注明: > Thread_Management