使用pthread创建子线程的简单步骤
  1. 导入头文件 #import <pthread.h>
  2. 指定新线程标识符
  3. 使用pthread创建线程的函数
  4. 根据result = 0 与否判断子线程创建成功与否
    对创建子线程的函数的简单解析
  • int pthread_create(pthread_t _Nullable * _Nonnull __restrict,
  • const pthread_attr_t * _Nullable __restrict,
  • void * _Nullable (* _Nonnull)(void * _Nullable),
  • void * _Nullable __restrict);

参数的意义分别为:

  • pthread_t _Nullable * _Nonnull __restrict          传入到线程标识符的指针地址
  • const pthread_attr_t * _Nullable __restrict        线程属性:传入指向线程属性的指针地址
  • void * _Nullable (* _Nonnull)(void * _Nullable)    新线程要执行的函数(任务),传入函数地址即函数名
  • void * _Nullable __restrict:                       传入到函数的参数
  • 返回值为整型                                         0表示创建线程成功 否则创建线程失败
  • 参数3的进一步解释

void * _Nullable    (* _Nonnull)   (void * _Nullable)

 
     函数返回值类型            函数名          函数参数
 
上代码
 
 
 #import "ViewController.h"
#import <pthread.h> @interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *pthreadCreateThreadBtn; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; }
#pragma mark - 创建线程的点击事件
- (IBAction)pthreadCreateThreadBtnClick:(id)sender { [self pthreadDemo];
[self pthreadDemo2]; } #pragma mark - pthread 创建子线程的代码实现
- (void)pthreadDemo{
//参数1: 新线程的标识符
pthread_t ID; //创建 指定标识符 指定参数为空 的线程
int result = pthread_create(&ID, NULL, demo, NULL);
if (result == ) {
NSLog(@"未传参数线程创建成功");
}else{
NSLog(@"未传参数线程创建失败");
} } #pragma mark - pthread 创建子线程的代码实现
- (void)pthreadDemo2{
// NSLog(@"pthreadDemo2 = %@",[NSThread currentThread]);
//参数1: 新线程的标识符
pthread_t id2;
NSString *ocStr = @"hello iOS"; //创建 指定标识符 指定参数 的线程
// Implicit conversion of Objective-C pointer type 'NSString *' to C pointer type 'void *' requires a bridged cast
int result = pthread_create(&id2, NULL, demo2, (__bridge void *)(ocStr));
if (result == ) {
NSLog(@"传了参数的线程创建成功");
}else{
NSLog(@"传了参数的线程创建失败");
} } #pragma mark - 创建的无参数新线程执行的函数
void *demo(void *param){
NSLog(@"用于创建无参数线程当前线程%@",[NSThread currentThread]);
return NULL;
} #pragma mark - 创建的有参数新线程执行的函数
void *demo2(void *param){
// Implicit conversion of Objective-C pointer type 'NSString *' to C pointer type 'void *' requires a bridged cast
NSString *str = (__bridge NSString *)(param); NSLog(@"用于创建的有参数%@的线程-当前线程%@",str,[NSThread currentThread]);
return NULL;
} @end

pthread创建线程的简单演示的更多相关文章

  1. 【转】用Pthread创建线程的一个简单Demo

    一.我们直接在COCOS2D-X自带的HelloWorld工程中添加代码.首先将Pthread的文件包含进来包括lib文件.在HelloWorld.cpp中引入头文件和库. #include &quo ...

  2. c++ Pthread创建线程后必须使用join或detach释放线程资源

    http://www.cppblog.com/prayer/archive/2012/04/23/172427.html 这两天在看Pthread 资料的时候,无意中看到这样一句话(man pthre ...

  3. Python_多线程1(创建线程,简单线程同步)

    threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法: threading.currentThread(): 返回当前的线程变量. threading.enumera ...

  4. C# 创建线程的简单方式:异步委托 .

    定义一个委托调用的方法:TakesAWhile //定义委托要引用的方法 private static int TakesAWhile(int data, int ms) { Console.Writ ...

  5. 创建线程方式-pthread

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  6. Java多线程学习总结--线程概述及创建线程的方式(1)

    在Java开发中,多线程是很常用的,用得好的话,可以提高程序的性能. 首先先来看一下线程和进程的区别: 1,一个应用程序就是一个进程,一个进程中有一个或多个线程.一个进程至少要有一个主线程.线程可以看 ...

  7. Netty源码分析第2章(NioEventLoop)---->第1节: NioEventLoopGroup之创建线程执行器

    Netty源码分析第二章: NioEventLoop 概述: 通过上一章的学习, 我们了解了Server启动的大致流程, 有很多组件与模块并没有细讲, 从这个章开始, 我们开始详细剖析netty的各个 ...

  8. Python创建线程

    Python 提供了 _thread 和 threading 两个模块来支持多线程,其中 _thread 提供低级别的.原始的线程支持,以及一个简单的锁,正如它的名字所暗示的,一般编程不建议使用 th ...

  9. posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序

    posix 线程(一):线程模型.pthread 系列函数 和 简单多线程服务器端程序 一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属 ...

随机推荐

  1. usaco training 4.2.4 Cowcycles 题解

    Cowcycles题解 Originally by Don Gillies [International readers should note that some words are puns on ...

  2. Java 容器在实际项目开发中应用

    前言:在java开发中我们离不开集合数组等,在java中有个专有名词:"容器" ,下面会结合Thinking in Java的知识和实际开发中业务场景讲述一下容器在Web项目中的用 ...

  3. web.xml is missing and <failOnMissingWebXml> is se

    摘要 maven模块化 在学习maven模块化构建项目的时候遇到了如下报错信息: web.xml is missing and <failOnMissingWebXml> is set t ...

  4. Android hook神器frida(一)

    运行环境 ● Python – latest 3.x is highly recommended ● Windows, macOS, or Linux安装方法使用命令 sudo pip install ...

  5. 面向对象15.3String类-常见功能-判断

    /*3.判断 * 3.1两个字符串内容是否相同? * boolean equals(Object obj)(参数是Object,不是String,因为equals是覆盖Object里面的equals方 ...

  6. Oozie时出现Exception in thread "main" java.lang.UnsupportedClassVersionError: com/mysql/jdbc/Driver : Unsupported major.minor version 52.0?

    不多说,直接上干货! 问题详情 [hadoop@bigdatamaster oozie--cdh5.5.4]$ bin/ooziedb.sh create -sqlfile oozie.sql -ru ...

  7. Linux系统——运行级别

    学习之前先了解下Linux系统的运行级别和其原理,博主使用的是Linux系统中的Redhat9.0版本,之后的学习也是基于这个系统版本. Linux系统的7个运行级别(runlevel) 运行级别0: ...

  8. hdu_1711: Number Sequence【KMP算法】

    题目链接 此次插播点笔记 hdu中点击蓝色的"Compilation Error"可以查看自己是为什么CE的 hdu中提交的话,语言选择G++可以使用<bits/stdc++ ...

  9. android - 解决“应用自定义权限重名”

    背景 现场的开发今天跟我说,测试包装不上!报错"应用自定义权限重名"!!! 网上百度下关键字,发现魅族手机有这个毛病,顺藤摸瓜:"http://bbs.flyme.cn/ ...

  10. UWP中使用Composition API实现吸顶(2)

    在上一篇中我们讨论了不涉及Pivot的吸顶操作,但是一般来说,吸顶的部分都是Pivot的Header,所以在此我们将讨论关于Pivot多个Item关联同一个Header的情况. 老样子,先做一个简单的 ...