pthreads簡介
引自 維基百科 實現(xiàn)POSIX 線程標(biāo)準(zhǔn)的庫常被稱作Pthreads获茬,一般用于Unix-like POSIX 系統(tǒng),如Linux呐赡、 Solaris究孕。但是Microsoft Windows上的實現(xiàn)也存在,例如直接使用Windows API實現(xiàn)的第三方庫pthreads-w32;而利用Windows的SFU/SUA子系統(tǒng),則可以使用微軟提供的一部分原生POSIX API。
其實锡溯,這就是一套在很多操作系統(tǒng)上都通用的多線程API,所以移植性很強(qiáng)哑姚,基于C封裝的一套線程框架趾唱,iOS上也是適用的。
Pthreads創(chuàng)建線程
- (void)onThread {
// 1\. 創(chuàng)建線程: 定義一個pthread_t類型變量
pthread_t thread;
// 2\. 開啟線程: 執(zhí)行任務(wù)
pthread_create(&thread, NULL, run, NULL);
// 3\. 設(shè)置子線程的狀態(tài)設(shè)置為detached蜻懦,該線程運行結(jié)束后會自動釋放所有資源
pthread_detach(thread);
}
void * run(void *param) {
NSLog(@"%@", [NSThread currentThread]);
return NULL;
}
打印結(jié)果:
<NSThread: 0x1c026f100>{number = 4, name = (null)}
如果出現(xiàn)pthread_create is invalid in C99
報錯甜癞,原因是沒有導(dǎo)入#import <pthread.h>
pthread_create(&thread, NULL, run, NULL);
- 第一個參數(shù)
&thread
是線程對象,指向線程標(biāo)識符的指針 - 第二個是線程屬性宛乃,可賦值
NULL
- 第三個
run
表示指向函數(shù)的指針(run
對應(yīng)函數(shù)里是需要在新線程中執(zhí)行的任務(wù)) - 第四個是運行函數(shù)的參數(shù)悠咱,可賦值
NULL
Pthreads其他相關(guān)方法
-
pthread_create()
:創(chuàng)建一個線程 -
pthread_exit()
:終止當(dāng)前線程 -
pthread_cancel()
:中斷另外一個線程的運行 -
pthread_join()
:阻塞當(dāng)前的線程蒸辆,直到另外一個線程運行結(jié)束 -
pthread_attr_init()
:初始化線程的屬性 -
pthread_attr_setdetachstate()
:設(shè)置脫離狀態(tài)的屬性(決定這個線程在終止時是否可以被結(jié)合) -
pthread_attr_getdetachstate()
:獲取脫離狀態(tài)的屬性 -
pthread_attr_destroy()
:刪除線程的屬性 -
pthread_kill()
:向線程發(fā)送一個信號
Pthreads常用函數(shù)與功能
-
pthread_t
用于表示Thread ID,具體內(nèi)容根據(jù)實現(xiàn)的不同而不同析既,有可能是一個Structure躬贡,因此不能將其看作為整數(shù)。 -
pthread_equal
函數(shù)用于比較兩個pthread_t是否相等眼坏。
int pthread_equal(pthread_t tid1, pthread_t tid2)
-
pthread_self
函數(shù)用于獲得本線程的thread id拂玻。
pthread _t pthread_self(void);
Pthreads實現(xiàn)互斥鎖
鎖可以被動態(tài)或靜態(tài)創(chuàng)建,可以用宏
PTHREAD_MUTEX_INITIALIZER
來靜態(tài)的初始化鎖宰译,采用這種方式比較容易理解檐蚜,互斥鎖是pthread_mutex_t
的結(jié)構(gòu)體,而這個宏是一個結(jié)構(gòu)常量沿侈,如下可以完成靜態(tài)的初始化鎖:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
也可以用pthread_mutex_init函數(shù)動態(tài)的創(chuàng)建闯第,函數(shù)原型如:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t * attr)
總共有100張火車票,開啟兩個線程缀拭,北京和上海兩個窗口同時賣票咳短,賣一張票就減去庫存,使用鎖蛛淋,保證北京和上海賣票的庫存是一致的咙好。實現(xiàn)如下。
#import "ViewController.h"
#include <pthread.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self onThread];
}
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
NSMutableArray *tickets;
- (void)onThread {
tickets = [NSMutableArray array];
//生成100張票
for (int i = 0; i < 100; i++) {
[tickets addObject:[NSNumber numberWithInt:i]];
}
//線程1 北京賣票窗口
// 1\. 創(chuàng)建線程1: 定義一個pthread_t類型變量
pthread_t thread1;
// 2\. 開啟線程1: 執(zhí)行任務(wù)
pthread_create(&thread1, NULL, run, NULL);
// 3\. 設(shè)置子線程1的狀態(tài)設(shè)置為detached褐荷,該線程運行結(jié)束后會自動釋放所有資源
pthread_detach(thread1);
//線程2 上海賣票窗口
// 1\. 創(chuàng)建線程2: 定義一個pthread_t類型變量
pthread_t thread2;
// 2\. 開啟線程2: 執(zhí)行任務(wù)
pthread_create(&thread2, NULL, run, NULL);
// 3\. 設(shè)置子線程2的狀態(tài)設(shè)置為detached勾效,該線程運行結(jié)束后會自動釋放所有資源
pthread_detach(thread2);
}
void * run(void *param) {
while (true) {
//鎖門,執(zhí)行任務(wù)
pthread_mutex_lock(&mutex);
if (tickets.count > 0) {
NSLog(@"剩余票數(shù)%ld, 賣票窗口%@", tickets.count, [NSThread currentThread]);
[tickets removeLastObject];
[NSThread sleepForTimeInterval:0.2];
}
else {
NSLog(@"票已經(jīng)賣完了");
//開門诚卸,讓其他任務(wù)可以執(zhí)行
pthread_mutex_unlock(&mutex);
break;
}
//開門,讓其他任務(wù)可以執(zhí)行
pthread_mutex_unlock(&mutex);
}
return NULL;
}
@end
打印結(jié)果:
對鎖的操作主要包括:
加鎖pthread_mutex_lock()
绘迁、
解鎖pthread_mutex_unlock()
和測試加鎖pthread_mutex_trylock()
三個合溺。
pthread_mutex_trylock()
語義與pthread_mutex_lock()
類似,不同的是在鎖已經(jīng)被占據(jù)時返回EBUSY
而不是掛起等待缀台。