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环境下的线程的创建问题的更多相关文章

  1. Linux环境下查看线程数的几种方法

    1.cat /proc/${pid}/status 2.pstree -p ${pid} 3.top -p ${pid} 再按H,或者直接输入 top -bH -d 3 -p  ${pid} top ...

  2. [转]Linux环境下查看线程数的几种方法

    1.cat /proc/${pid}/status 2.pstree -p ${pid} 3.top -p ${pid} 再按H,或者直接输入 top -bH -d 3 -p  ${pid} top ...

  3. Linux环境下,使用PHP创建一个守护进程

    <?php $pid = pcntl_fork(); // fork if ($pid < 0) exit; else if ($pid) // parent exit; else { / ...

  4. 多线程编程之Linux环境下的多线程(一)

    一.Linux环境下的线程 相对于其他操作系统,Linux系统内核只提供了轻量级进程的支持,并未实现线程模型.Linux是一种“多进程单线程”的操作系统,Linux本身只有进程的概念,而其所谓的“线程 ...

  5. Linux环境下C语言线程创建---简单代码

    在Linux环境下用C语言编写线程创建. //file name: pthreadtext.c #include <stdio.h> #include <pthread.h> ...

  6. 多线程编程之Linux环境下的多线程(三)

    前面两篇文章都讲述了Linux环境下的多线程编程基础知识,也附带了典型实例.本文主要比较一下Linux环境与Windows环境下的多线程编程区别. 看待技术问题要瞄准其本质,不管是WIN32.Linu ...

  7. 多线程编程之Linux环境下的多线程(二)

    上一篇文章中主要讲解了Linux环境下多线程的基本概念和特性,本文将说明Linux环境下多线程的同步方式. 在<UNIX环境高级编程>第二版的“第11章 线程”中,提到了类UNIX系统中的 ...

  8. mosquitto在Linux环境下的部署/安装/使用/测试

    mosquitto在Linux环境下的部署 看了有三四天的的源码,(当然没怎么好好看了),突然发现对mosquitto的源码有了一点点感觉,于是在第五天决定在Linux环境下部署mosquitto. ...

  9. linux环境下使用jmeter进行压力测试

    linux环境下使用jmeter进行压力测试 linux环境下使用就meter进行压力测试: linux环境部署: 在Linux服务器先安装jdk: 2.以jdk-8u172-linux-x64.ta ...

随机推荐

  1. vc2010下使用64位控件

    最近把我的控件(ST_Curve www.st-curve.cn)升级到了64位,2010编译,本来以为很简单的问题,结果折腾了两天(也有可能我多年没做过界面和vc相关的东西了吧),于是把我遇到的问题 ...

  2. hdu 4704 同余定理+普通快速幂

    此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...

  3. 动态规划-hdoj-4832-百度之星2014初赛第二场

    Chess Problem Description 小度和小良近期又迷上了下棋.棋盘一共同拥有N行M列,我们能够把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王 ...

  4. Spring中的FactoryBean

    从SessionFactory说起: 在使用SSH集成开发的时候,我们有时候会在applicationContext.xml中配置Hibernate的信息,以下是配置SessionFactory的一段 ...

  5. Linux学习记录--匿名沟通渠道

    匿名沟通渠道 管道Linux最初支持Unix IPC其中的一种形式.具有下列特征: 1.管道是半双工.数据可以仅在一个方向流动:当双方需要沟通.建设两条管线需要. 2.仅仅能用于父子进程或者兄弟进程之 ...

  6. [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动

    cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...

  7. K-means clustering (K-means聚类)

    问题: K-所有值聚类是无监督学习算法 设数据集.当中,. 如果这个数据能够分为类. 把这个问题模型化: , 当中代表第类的聚点(中心点.均值). 该模型能够用EM算法进行训练: 初始化,. E步:固 ...

  8. Java输出当前的日期(年月日时分秒毫秒)

    package test.remote.tools.combine; import java.text.SimpleDateFormat; import java.util.Calendar; imp ...

  9. ifconfig 源码

    贴一下ifconfig的源码(它属于net-tool软件包),以备平时查看网络信息的配置. /* * ifconfig This file contains an implementation of ...

  10. HDU 4921 Map

    题意: 给n个节点  他们形成了最多10条链  每条最多1000的长度  每一个节点有个val  你能够选择任何位置截断链  断点前的全部节点被你获得  通过题中计算公式得出你的val  问  通过随 ...