线程阻塞 pthread

主线程执行耗时操作 阻塞主线程

1-主线程执行耗时操作阻塞用户与UI的交互.png
- (IBAction)buttonClick {
    for (NSInteger i = 0; i<50000; i++) {
        NSLog(@"------buttonClick---%zd", i);
    }
}

用户点击了按钮,在打印完50000个数字之前,用户与页面UI控件的交互被阻塞

pthread

#import "ViewController.h"
#import <pthread.h>

@interface ViewController ()

@end

@implementation ViewController

void * run(void *param)
{
    for (NSInteger i = 0; i<50000; i++) {
        NSLog(@"------buttonClick---%zd--%@", i, [NSThread currentThread]);
    }
    return NULL;
}

- (IBAction)buttonClick:(id)sender {
    pthread_t thread;
    pthread_create(&thread, NULL, run, NULL);
    
    pthread_t thread2;
    pthread_create(&thread2, NULL, run, NULL);
}

@end

pthread:纯C
一套通用的多线程API
适用于Unix\Linux\Windows等系统
跨平台\可移植
使用难度大,程序员管理线程生命周期,在实际开发中几乎不用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转自:Youtherhttps://www.cnblogs.com/youtherhome/archive/201...
    njukay阅读 1,640评论 0 52
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,049评论 25 709
  • Object C中创建线程的方法是什么?如果在主线程中执行代码,方法是什么?如果想延时执行代码、方法又是什么? 1...
    AlanGe阅读 1,835评论 0 17
  • iOS中,只有主线程跟Cocoa关联,也即是在这个线程中,更新UI总是有效的,如果在其他线程中更新UI有时候会成功...
    mengyingguo阅读 554评论 0 0
  • 小海的哥 所有的见地从疑惑中来 一个好的问题 往往会引发一层一层的讨论和思考 比如 地球上的婚姻制度还会持续多久?...
    l小南阅读 720评论 0 0