pthread_create函数

创建线程的函数
int pthread_create(pthread_t *pid,const pthread_att_t *attr,void *(*start_routine)(void*),void *arg)

  • pid:指针,指向创建成功的新线程ID
  • attr:指向线程属性结构pthread_attr_t的指针,如果为NULL,则是默认属性,默认属性为线程非分离,大小为1Mb的堆栈,与父线程有相同的优先级。
  • start_routine:线程的回调函数
  • arg:回调函数的参数信息

创建成功返回0,其余返回值都为创建失败

#include<pthread.h>
#include<iostream>
#include<unistd.h>using namespace std;void *func(void* arg)
{cout<<"pthread_creat func ~~~~"<<endl;return 0;
}int main()
{pthread_t tidp; //线程的Idint ret;ret = pthread_create(&tidp,NULL,func,NULL); if (ret){cout<<"pthead_creat error"<<endl;return -1;}sleep(10);cout<<"pthread_creat success"<<endl;return 0;
}

pthread_create函数传参

按照字符串传参

在按照字符串进行传参时,需要使用常量字符串。

#include<pthread.h>
#include<iostream>using namespace std;void *func(void* arg)
{char *ret;ret  = (char*) arg;cout<<ret<<endl;cout<<"new pthread create success"<<endl;
}int main()
{pthread_t tidp;int ret;const char *str = "hello world";ret = pthread_create(&tidp,NULL,func,(void *)str);if(ret){cout<<"create pthread error"<<endl;return -1;}pthread_join(tidp,NULL);cout<<"create pthread success"<<endl;return 0;
}

按照结构体传参

#include<pthread.h>
#include<iostream>using namespace std;typedef struct{int n;const char* str;
}MyStruct;void *func(void *arg)
{MyStruct *p = (MyStruct*)arg;cout<<p->n<<endl;cout<<p->str<<endl;cout<<"new pthread create success"<<endl;
}int main()
{pthread_ t tidp;int ret;MyStruct mystruct;//定义一个结构体//初始化结构体mystruct.n = 10;mystruct.str = "hello world"; //常量字符串赋值给常量ret = pthread_create(&tidp,NULL,func,(void*)&mystruct);if(ret){cout<<"pthead_creat error"<<endl;return -1;}pthread_join(tidp,NULL);cout<<"pthread_creat success"<<endl;return 0;
}

pthread_join函数

线程等待函数,由于创建出来的新线程有可能会比主线程慢,因此,我们需要等待新线程执行完毕后,再退出主线程。

#include<pthread.h>
#include<iostream>using namespace std;void *func(void* arg)
{cout<<"new pthread create success"<<endl;
}int main()
{pthread_t tidp;int ret;ret = pthread_create(&tidp,NULL,func,NULL);if(ret){cout<<"pthead_creat error"<<endl;return -1;}pthread_join(tidp,NULL);cout<<"pthread_creat success"<<endl;return 0;
}

pthread_join 会一直等待子线程结束后才执行函数后面的代码,第一个参数为子线程的ID,第二个参数为线程退出码,如果不关注则可以设置为NULL。

获取线程属性pthread_getattr_np

线程的属性包括分离状态,调度策略,参数,作用域,栈等等,都存放在该联合体中。我们查看会特别不方便,因为Linux为我们提供了一个方法。int pthread_getattr_np(pthread_t pid,pthread_attr_t *attr)

  • pid:为线程的Id
  • attr:为线程属性结构体的内容

如果执行成功就返回0

初始化线程属性pthread_attr_init

我们在使用pthread_create函数创建线程的时候,第二个参数默认为NULL,如果我们想要自己对线程的一下属性进行修改,则需要使用pthread_attr_init函数。
int pthread_attr_init(pthread_attr_t *attr);

设置线程分离属性

线程默认为一个连接的,其属性为joinable,一个处于连接属性的线程,是不会自己回收资源,必须等待其他线程来回收其资源。如果父进程没有调用pthread_join来进行等待的话,处于连接的线程资源就一直没有被释放,会编程僵尸线程,使得资源被浪费。

一个线程只能被一个线程等待。

int pthread_attr_setdatachstate(pthread_attr_t *attr,int detachstate

  • detachstate:有两种选项:PTHREAD_CREATE_JOINABLEPTHREAD_CREATE_DETACHED
//实现一个可以分离的线程
#include<pthread.h>
#include<iostream>
#include<unistd.h>using namespace std;void *func(void* arg)
{cout<<("pthread callback func\n");return NULL;
}int main()
{pthread_t pid;pthread_attr_t pattr;int ret ;ret = pthread_attr_init(&pattr);if(ret){cout<<"pthread init error"<<endl;return -1;}ret = pthread_attr_setdetachstate(&pattr,PTHREAD_CREATE_DETACHED);if(ret){cout<<"pthread set detach error"<<endl;return -1;}ret = pthread_create(&pid,&pattr,func,NULL);if(ret){cout<<"pthread create error"<<endl;return -1;}cout<<"main pthread will exit"<<endl;sleep(1);//sleep1:确保子线程执行,但是我们不确定sleep多少,子线程才会执行,因此我们可以使用pthread_exit//pthread_exit(NULL)return 0;
}

pthread_exit线程退出函数,在主线程种执行,并不会导致进程退出,等到所有的子线程退出后,进程才会退出。

线程分离的优缺点

  • 线程分离后线程的属性为detach,在退出之后,系统会将其占用的资源自动释放掉,因此,处于分离的线程不能被其他线程回收或杀死。
  • 处于joinable属性的线程,需要别的线程使用join_able来进行等待,其资源才会被释放。

线程退出

线程退出的场景主要有4种:

  • 调用pthread_exit函数
  • 调用return函数
  • 线程被其他线程通知结束
  • 进程结束

Linux_c++线程函数的使用相关推荐

  1. C++多线程之间,线程函数启动之后,多线程依赖的启动和线程唤醒操作。

    C++多线程之间,线程函数启动之后,线程间依赖的启动和唤醒操作 一.原理分析 1. 线程依赖关系 二. 实例分析 2.1 多线程启动 2.2 多线程模式讲解 (1) 多线程开启与主线程唤醒 (2)单线 ...

  2. 第三节 线程传参详解、detach()大坑、成员函数做线程函数

    1.传递临时对象作为线程参数 原始的输入程序如下: #include <iostream> #include <thread>using namespace std;void ...

  3. 【Linux 线程】常用线程函数复习《一》

    1.pthread_create以及pthread_self函数 1 /**************************************************************** ...

  4. CreateThread创建线程函数详细讲解

    CreateThread CreateThread函数创建一个要在调用进程的地址空间中执行的线程.(MSDN讲解如下) 处理CreateThread ( LPSECURITY_ATTRIBUTES l ...

  5. C++类中封装线程函数

    线程函数必须声明为静态 非静态函数都要有一个隐含参数,作为this指针 而线程函数只能是普通函数,要写在类里面一定要是静态的,然后把类指针作为参数进行显式传递 class MyClass {publi ...

  6. 线程函数的设计以及MsgWaitForMultipleObjects函数的使用要点

    使用多线程技术可以显著地提高程序性能,本文就讲讲在程序中如何使用工作线程,以及工作线程与主线程通讯的问题. 一 创建线程 使用MFC提供的全局函数AfxBeginThread()即可创建一个工作线程. ...

  7. linux中的线程函数

    函数pthread_create  作用:创建线程  函数原型:int pthread_create(pthread_t * tidp,const pthread_attr_t*attr,void*( ...

  8. Linux / pthread_create() 函数所使用的线程函数为什么必须是静态函数?

    答案:因为 pthread_create() 函数要求的线程函数必须满足如下格式: void *ThreadFunc(void *args); 对于普通类成员函数.虚函数,他们实际上都是包含了调用他们 ...

  9. Linux学习之系统编程篇:创建线程函数

    所有线程函数相同点: (1)函数调用成功返回 0,失败返回错误号(但注意:不能用 perror 打印). (2) 头文件:#include <pthread.h> 编译时候需要使用线程库, ...

最新文章

  1. linux命令:case选择结构语句
  2. 2019年Vue学习路线图
  3. 探测器反向偏压_Y5T60 为何探测器和电吸收调制器,加反电压,而不是正电压
  4. 【计算机系统设计】学习笔记(1)03,04
  5. 中传计算机学院考研笔记,中传考研 新闻编辑重点笔记 整理版.doc
  6. AdapterViewlt;?gt; arg0, View arg1, int arg2, long arg3參数含义
  7. python关系运算符实例_python编程中最常用的比较运算符实例
  8. mod_rewrite模块的使用
  9. 阿里巴巴矢量字体转Image图片(建议收藏)
  10. ps换证件照背景颜色
  11. 网站服务器怎么查ipv4,服务器的ipv4地址怎么查
  12. 实现webIM技术小结——websocket IM原理篇
  13. CSS第四篇(复合选择器)
  14. a java programe about tcp server
  15. 嵌入式Linux容器技术
  16. CSS单行/多行文本溢出显示省略号(...)
  17. 关于memset,malloc以及free后的野指针误区详解
  18. CF 1562 C. Rings (思维+模拟)
  19. 4g+uim卡是什么卡_从 2G 到 4G,中国电信天翼 UIM 手机卡图赏
  20. Android性能优化(二)—— 内存优化

热门文章

  1. Python机器学习全流程项目实战精讲(2018版)
  2. 搜索关键字高亮显示,就比微信多个多音字搜索
  3. CCIE-交换路由复习笔记
  4. POJ3264Balanced Lineup(线段树)
  5. 机器学习实现线性梯度算实现octave
  6. 路由器-配置(思科)
  7. JAVA Pattern和Matcher 的用法
  8. Apache服务器全局配置之服务器标识配置篇
  9. 满分的项目经理,个人觉得很有道理,所以[转载]
  10. SpringBoot 的属性配置文件