linux环境下的线程的创建问题
pthread_create函数用于创建一个线程
函数原型
#include<pthread.h>
int pthread_create(pthread_t *restrict tidp,
const pthread_attr_t *restrict attr,
void *(*start_rtn)(void *),
void *restrict arg);
參数与返回值
tidp:类型为pthread_t的指针,当pthread_create成功返回时,该函数将线程ID存储在tidp指向的内存区域中
pthread_t:typedef unsigned long int pthread_t 。64位环境中是8字节无符号数。32位环境中是4字节无符号数
參数与返回值:
attr:用于定制各种不同的线程属性。
通常可设为NULL,採用默认线程属性
start_rtn:线程的入口函数。即新创建的线程从该函数開始运行。
该函数仅仅有一个參数,即arg。返回一个指针
arg:作为start_rtn的第一个參数
成功返回0,出错时返回各种错误码
restrictkeyword是C99标准引入的,仅仅能用于限定指针。表明指针是訪问一个数据对象的唯一且初始的方式
当中的cpp文件为
#include<pthread.h>
#include<iostream>
#include<unistd.h>
using namespace std;
void *thread(void *arg)
{
sleep(5);
long i = (long)arg;
cout << "in thread, tid = " << pthread_self() << endl;
cout << "arg is " << i << endl; return (void *)0;
}
int main()
{
pthread_t tid;
if(pthread_create(&tid, NULL, thread, (void *)2) != 0)
{
cout << "pthread_create error" << endl;
return 0;
}
return 0;
}
程序的结果是没有不论什么输出,其原因在于主线程先于新创建的线程退出,于是能够思考什么是主线程,怎么办?
解决方法是让主线程睡眠一段时间
>>pthread_join函数用于等待某个线程终止
函数原型
#include<pthread.h>
int pthread_join(pthread_t thread,
void **rval_ptr);
调用该函数的线程将一直堵塞。直到指定的线程退出
返回值与參数:
成功返回0,否则返回错误编号
thread:须要等待的线程ID
rval_ptr:
返回线程的退出码
若不关心线程返回值,可将该參数设置为NULL
#include<pthread.h>
#include<iostream>
#include<unistd.h>
using namespace std;
void *thread(void *arg)
{
sleep(5);
long i = (long)arg;
cout << "in thread, tid = " << pthread_self() << endl;
cout << "arg is " << i << endl; return (void *)0;
} int main()
{
pthread_t tid;
if(pthread_create(&tid, NULL, thread, (void *)2) != 0)
{
cout << "pthread_create error" << endl;
return 0;
}
pthread_join(tid, 0);//与上面程序的差别
return 0;
}
程序的结果为:
in thread, tid = 140125960128256
arg is 2
>>在默认情况下。线程的终止状态会保存到对该线程调用pthread_join
若线程已经处于分离状态。线程的底层存储资源能够在线程终止时马上被收回
当线程被分离时。并不能用pthread_join函数等待它的终止状态,此时pthread_join返回EINVAL
pthread_detach函数能够使线程进入分离状态
函数原型
#include<pthread.h>
int pthread_detach(pthread_t tid);
參数与返回值
tid:进入分离状态的线程的ID
成功返回0。出错返回错误编号
以下的实例:
若pthread_join比pthread_detach先调用,也能获取到退出信息
#include<pthread.h>
#include<iostream>
#include<unistd.h>
#include<errno.h>
using namespace std;
void *thread(void *arg)
{
cout << "in thread, tid = " << pthread_self() << endl;
sleep(2);
pthread_detach(pthread_self());
cout << "Hello World!" << endl;
sleep(2);
return (void *)0;
} int main()
{
pthread_t tid;
if(pthread_create(&tid, NULL, thread, 0) != 0)
{
cout << "pthread_create error" << endl;
return 0;
}
//sleep(2);2秒的话就没问题,4秒就join失败,2秒的时候<span style="font-size:12px;">pthread_join还是比pthread_detach先调用的</span>//cout<<"thread的值在上面"<< endl;
int *r;
int s = pthread_join(tid, (void **)&r);
if(s == EINVAL)
{
cout << "join error" << endl;
}
else
{
cout<<"r的值" << r << endl;
} cout << "in main thread, tid = " << pthread_self() << endl; return 0;
}
linux环境下的线程的创建问题的更多相关文章
- Linux环境下查看线程数的几种方法
1.cat /proc/${pid}/status 2.pstree -p ${pid} 3.top -p ${pid} 再按H,或者直接输入 top -bH -d 3 -p ${pid} top ...
- [转]Linux环境下查看线程数的几种方法
1.cat /proc/${pid}/status 2.pstree -p ${pid} 3.top -p ${pid} 再按H,或者直接输入 top -bH -d 3 -p ${pid} top ...
- Linux环境下,使用PHP创建一个守护进程
<?php $pid = pcntl_fork(); // fork if ($pid < 0) exit; else if ($pid) // parent exit; else { / ...
- 多线程编程之Linux环境下的多线程(一)
一.Linux环境下的线程 相对于其他操作系统,Linux系统内核只提供了轻量级进程的支持,并未实现线程模型.Linux是一种“多进程单线程”的操作系统,Linux本身只有进程的概念,而其所谓的“线程 ...
- Linux环境下C语言线程创建---简单代码
在Linux环境下用C语言编写线程创建. //file name: pthreadtext.c #include <stdio.h> #include <pthread.h> ...
- 多线程编程之Linux环境下的多线程(三)
前面两篇文章都讲述了Linux环境下的多线程编程基础知识,也附带了典型实例.本文主要比较一下Linux环境与Windows环境下的多线程编程区别. 看待技术问题要瞄准其本质,不管是WIN32.Linu ...
- 多线程编程之Linux环境下的多线程(二)
上一篇文章中主要讲解了Linux环境下多线程的基本概念和特性,本文将说明Linux环境下多线程的同步方式. 在<UNIX环境高级编程>第二版的“第11章 线程”中,提到了类UNIX系统中的 ...
- mosquitto在Linux环境下的部署/安装/使用/测试
mosquitto在Linux环境下的部署 看了有三四天的的源码,(当然没怎么好好看了),突然发现对mosquitto的源码有了一点点感觉,于是在第五天决定在Linux环境下部署mosquitto. ...
- linux环境下使用jmeter进行压力测试
linux环境下使用jmeter进行压力测试 linux环境下使用就meter进行压力测试: linux环境部署: 在Linux服务器先安装jdk: 2.以jdk-8u172-linux-x64.ta ...
随机推荐
- c/c++ 动态申请数组
new和delete运算符用于动态分配和撤销内存的运算符 new使用方法: 1. 开辟单变量地址空间 1)new int; //开辟一个存放数组的存储空间,返回一个指向该存储空间的地址.in ...
- 配置BeanUtils包,同时也是对导入第三包的步骤说明
BeanUtils是由Apache公司开发的针对操作JavaBean的工具包. 对于JavaBean,简单的来说,就是要有一个空参的构造器和对属性的getXXX方法和setXXX方法. 在由JDK提供 ...
- delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)
//from http://kingron.myetang.com/zsfunc0d.htm (*// 标题:充分利用pascal字符串类型 说明:和PChar不同,string可以保存# ...
- 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成
原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...
- c#(winform,webform通用)利用npoi将xls文件复制为xlsx文件(excel的修改,保存,包括excel2003-office2007+的处理)
1.程序界面 每次需要处理excel文件的时候,都是去百度找方案,真是气一头火,今天好好总结一下,下次就不用度娘了. 我是用winform来试验的,因为winform比较方便测试,实际上只要是在.ne ...
- Android入门第六篇之ListView (一)
本文来自http://blog.csdn.net/hellogv/ ListView是一个经经常使用到的控件,ListView里面的每一个子项Item能够使一个字符串,也能够是一个组合控件.先说说Li ...
- [ExtJS5学习笔记]第第二十四次 Extjs5形式上gridpanel或表单数据后台传输remoteFilter设定
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39667533 官方文档:http://docs.sencha.com/extjs/5. ...
- Delphi的组件读写机制
Delphi的组件读写机制(一) 一.流式对象(Stream)和读写对象(Filer)的介绍在面向对象程序设计中,对象式数据管理占有很重要的地位.在Delphi中,对对象式数据管理的支持方式是其一大特 ...
- urllib2的异常处理
异常处理 作为爬虫的抓取过程基本就那么多内容了,后面再将一些正则表达式的东西简单介绍一下基本就完事了,下面先说说异常处理的方法.先介绍一下抓取过程中的主要异常,如URLError和HTTPError. ...
- Common lisp菜鸟指南(译)
Common lisp菜鸟指南(译) Common lisp菜鸟指南(译)