你的位置:首页 > 软件开发 > 操作系统 > 4.1 Pthread 4.2 NSThread

4.1 Pthread 4.2 NSThread

发布时间:2016-03-25 12:00:15
因为Pthread很少用到,所以对于Pthread的知识没有抠那么细致,所以将Pthread和 NSThread放在了一起。 4.1 Pthread4.1-1.0 创建线程 - pthread_create 1 /* 2 <#pthread_t *restrict#&g ...

4.1 Pthread      4.2 NSThread

因为Pthread很少用到,所以对于Pthread的知识没有抠那么细致,所以将Pthread和 NSThread放在了一起。

 

4.1 Pthread


4.1-1.0 创建线程 - pthread_create

 1 /* 2 <#pthread_t *restrict#> 线程的id,指向线程标识符的指针,C 语言中类型的结尾通常 _t/Ref,而且不需要使用 * 3 <#const pthread_attr_t *restrict#> 用来设置线程的属性 (一般为 NULL) 4 <#void *(*)(void *)#> 新建立的线程执行代码的函数,线程开启后需要调用的函数或方法 (指向函数的指针) 5 <#void *restrict#>   运行函数的参数,线程的限制 (一般为 NULL) 6 */ 7  8 返回值: 9 - 若线程创建成功,则返回010 - 若线程创建失败,则返回出错编号11 12 pthread_create(13      <#pthread_t *restrict#>,  // pthread_t :线程标识符.14      <#const pthread_attr_t *restrict#>,15      <#void *(*)(void *)#>,16      <#void *restrict#>17 );
  • 在 C 语言中,没有对象的概念,对象是以结构体的方式来实现的
  • 通常,在 C 语言框架中,对象类型以 _t/Ref 结尾,而且声明时不需要使用 *
  • C 语言中的 void * 和 OC 中的 id 是等价的
  • 在混合开发时,如果在 C 和 OC 之间传递数据,需要使用 __bridge 进行桥接,
  • 桥接的添加可以借助 Xcode 的辅助功能添加
 
 1 #import "ViewController.h" 2  3 @interface ViewController () 4 @end 5  6 @implementation ViewController 7  8 - (void)viewDidLoad { 9  [super viewDidLoad];10  // Do any additional setup after loading the view, typically from a nib.11 }12 13 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {14  [self threadDemo1];15 }16 17 #pragma mark - 对象方法alloc/init18 /*19  - 在 OC 中,任何一个方法的代码都是从上向下顺序执行的20  - 同一个方法内的代码,都是在相同线程执行的(block除外)21 */22 - (void)threadDemo1 {23  NSLog(@"before %@", [NSThread currentThread]);24 25  // 线程一启动,就会在线程thread中执行self的run方法26  NSThread *thread = [[NSThread alloc] initWithTarget:self27                        selector:@selector(longOperation:)28                        object:@"THREAD"];29 30  //开启线程,通过start方法,就会将我们创建出来的当前线程加入到`可调度线程池`,供CPU调度31  //[thread start];执行后,会在另外一个线程执行 longOperation: 方法32  [thread start];33 34  NSLog(@"after %@", [NSThread currentThread]);35 }36 37 - (void)longOperation:(id)obj {38  NSLog(@"%@ - %@", [NSThread currentThread], obj);39 }40 41 - (void)didReceiveMemoryWarning {42  [super didReceiveMemoryWarning];43  // Dispose of any resources that can be recreated.44 }45 46 @end
4.2-1.2 创建线程2 - 类方法
1 创建方式2 : 通过NSThread的类方法 (创建线程后直接自动启动线程)2 3 [NSThread detachNewThreadSelector:<#(nonnull SEL)#>4             toTarget:<#(nonnull id)#>5            withObject:<#(nullable id)#> ];
 生命周期示意图:  
打印结果:2016-03-17 19:37:27.429 线程安全[3386:472835] 售票员02卖了一张票,还剩下9张2016-03-17 19:37:27.430 线程安全[3386:472836] 售票员03卖了一张票,还剩下8张2016-03-17 19:37:27.430 线程安全[3386:472834] 售票员01卖了一张票,还剩下7张2016-03-17 19:37:27.430 线程安全[3386:472835] 售票员02卖了一张票,还剩下6张2016-03-17 19:37:27.430 线程安全[3386:472836] 售票员03卖了一张票,还剩下5张2016-03-17 19:37:27.430 线程安全[3386:472834] 售票员01卖了一张票,还剩下4张2016-03-17 19:37:27.431 线程安全[3386:472835] 售票员02卖了一张票,还剩下3张2016-03-17 19:37:27.431 线程安全[3386:472836] 售票员03卖了一张票,还剩下2张2016-03-17 19:37:27.431 线程安全[3386:472834] 售票员01卖了一张票,还剩下1张2016-03-17 19:37:27.431 线程安全[3386:472835] 售票员02卖了一张票,还剩下0张2016-03-17 19:37:27.432 线程安全[3386:472836] 票已经卖完了2016-03-17 19:37:27.434 线程安全[3386:472834] 票已经卖完了2016-03-17 19:37:27.434 线程安全[3386:472835] 票已经卖完了
  4.2-5.1 图片下载示例如有疑问,请发送邮件至 shorfng@126.com 联系我。

原标题:4.1 Pthread 4.2 NSThread

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录