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 ...
随机推荐
- hdu 4687 带花树匹配
其实吧,思路挺简单的,只不过昨天刚学,还有一些东西不太了解,然后就23333333... 吃晚饭回来就A了,我是有多傻啊,这么题都A不掉,不能忍啊... 我们可以先求出哪些边是可能存在于最大匹配中的, ...
- mysql基础:列类型--整型
mysql列类型--字符串 http://blog.csdn.net/jk110333/article/details/9342301 mysql列类型--时间和日期 http://blog. ...
- C++ 虚函数表解析(比较清楚,还可打印虚函数地址)
C++ 虚函数表解析 陈皓 http://blog.csdn.net/haoel 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父 ...
- OpenRisc-30-SD card controller模块分析与验证
引言 ORPSoC的硬件平台是包含SD card controller控制器的,但是对应的linux里面却没有对应的linux的驱动程序,这使ORPSoC的SD card的使用收到了很大的限制.没有驱 ...
- Floodlight 处理交换机增加/移除过程
Floodlight 使用的是Netty架构,在Controller.java 入口函数中显示创建ServerBootstrap,设置套接字选项,ChannelPipeline,此时监听套接 ...
- jquery 设置select的默认值
<select id="sel" > <option value="s1" > aaaa </option> <opt ...
- maven使用.02.一些概念
在上一篇POST中,简要的介绍了一下maven的特点,优势,安装.并建立了一个简单地Hello world工程.这一篇POST中,将主要会介绍一下Maven的一些约定. pom.xml文件 Maven ...
- VC++ 视频播放器 图文步骤记录
1.安装DirectShow9.0 SDK DirectShow9 SDK下载链接http://download.csdn.net/detail/jindou910101/5591169 2.运行Di ...
- wkhtmtopdf--高分辨率转HTML成PDF--目录篇
原文:wkhtmtopdf--高分辨率转HTML成PDF--目录篇 wkhtmtopdf--高分辨率转HTML成PDF(一):简述wkhtmtopdf的简介和安装 wkhtmtopdf--高分辨率转H ...
- Android设计模式(二)--策略模式
1.定义: The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them inter ...