pthread使用

引言: 针对上一篇文章的 线程的概念 的理论知识的实践

第一步:

#import 

第二步(创建线程):

 /**
    方法功能: 使用pthread创建线程的方法

    参数1:  线程的地址编号
    参数2:  线程的属性
    参数3:  " void * (*) (void *) " 线程要执行的函数
    参数4:   要执行的函数的参数
     
    返回值:  返回int类型 0表示成功 非零表示失败
     */
    
    pthread_t pthread; //线程编号
    
   int result = pthread_create(&pthread, NULL, testpthread, (__bridge void *)(createrName));
    
    if (result == 0) {
        NSLog(@"成功");
    }else {
        NSLog(@"失败");
    }
    
     /*
     注意事项:
     1、函数名就是函数地址
     2、nil 是空对象 NULL是空指针
     3、void * 指向任何类型的指针 有点类似OC中的id;
     4、把OC对象传给c语言函数要桥接(__brige) 反之同理
     */


    
    // testpthread 是传入C语言函数

    NSString *createrName = (__bridge NSString *)(params);
    
    // {number = 3, name = (null)} 先创建的是子线程
    NSLog(@"线程创建者--%@ 当前的线程 -- %@", createrName, [NSThread currentThread]);
    return NULL;
 
    
  • 附上demo地址方便测试学习

  • http://git.oschina.net/iOS_dper/PhtreadDemo

你可能感兴趣的:(pthread使用)