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 ...
随机推荐
- <摘录>详谈高性能TCP服务器的开发
对于开发一款高性能服务器程序,广大服务器开发人员在一直为之奋斗和努力.其中一个影响服务器的重要瓶颈就是服务器的网络处理模块.如果一款服务器程序不能及时的处理用户的数据.则服务器的上层业务逻辑再高效也是 ...
- 《转载》常用算法经典代码(C++版)
转自:http://blog.renren.com/blog/311453043/736944237 一.快速排序 void qsort(int x,int y) //待排序的数据存放在a[1]..a ...
- linux下修改hostid
linux下修改hostid 网上有很多版本,总结了这几点. 1> 一个以16进制显示的int字符串: 2> 配置文件: /etc/hostid; 如果有值,输出, 结束. 3> 从 ...
- vc笔记六
通知消息(Notification message)是指这样一种消息,一个窗口内的子控件发生了一些 事情,需要通 知父窗口.通知消息只适用于标准的窗口控件如按钮.列表框.组合框.编辑框,以及 Wind ...
- 利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)
利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)作者:Tuuzed(土仔) 发表于:2008年3月3日23:12:38 版权声明:可以任意转载,转载时请 ...
- SLB 权重问题
<pre name="code" class="html">一般配置SLB的时候有个权重0到100,是如何选择数值的? 权重需要您根据后端机器的配置 ...
- mysql+ssh整合样例,附源代码下载
项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727 项目源代码下载地址:http://download.csdn.net/detail/ ...
- Spring MVC Hello World Example(转)
Spring 3 You may interest at this Spring 3 MVC hello world example. In Spring MVC web application, i ...
- 《Java并发编程实战》第十六章 Java内存模型 读书笔记
Java内存模型是保障多线程安全的根基,这里不过认识型的理解总结并未深入研究. 一.什么是内存模型,为什么须要它 Java内存模型(Java Memory Model)并发相关的安全公布,同步策略的规 ...
- nagios二次开发(五岁以下儿童)---nagios和nagiosql关系
基于nagios和nagiosql理解.这将是这两个梳理比较粗糙的简单关系,有关详细信息,请参阅下面的图如: 从上面的关系图中能够看出,nagios与nagiosql共享了主机.主机组.服务 ...