现在的位置: 首页 > 综合 > 正文

linux环境下的线程的创建问题

2018年01月16日 ⁄ 综合 ⁄ 共 2446字 ⁄ 字号 评论关闭

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,出错时返回各种错误码

restrict关键字是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;
}


抱歉!评论已关闭.