iOS多线程

iOS多线程分为四种

  • Pthreads
  • NSThread
  • GCD
  • NSOperation & NSOperationQueue

    Pthreads

    POSIX线程(POSIX threads),简称Pthreads,是线程的POSIX标准。该标准定义了创建和操纵线程的一整套API。在类Unix操作系统(Unix、Linux、Mac OS X等)中,都使用Pthreads作为操作系统的线程。

    简单地说,这是一套在很多操作系统上都通用的多线程API,所以移植性很强(然并卵),当然在 iOS 中也是可以的。不过这是基于C语言的框架,使用起来比较酸爽啦!感受一下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    pthread_t thread;
    //创建一个线程并自动执行
    pthread_create(&thread, NULL, start, NULL);
    }
    void *start(void *data) {
    NSLog(@"%@", [NSThread currentThread]);
    return NULL;
    }

打印并输出:

1
2
2015-07-27 23:57:21.689 testThread[10616:2644653]
<NSThread: 0x7fbb48d33690>{number = 2, name = (null)}


您是本博客的第个小伙伴