Liunx系统编程篇—多线程编程(二)线程开发常用API(一)(关于线程操作)(共3种全)

线程开发常用API——关于线程操作

包含的头文件
#include <pthread.h>

线程操作:

线程操作包括3 种线程的创建,退出,等待。

1、线程的创建
函数原型
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
函数参数

tidp:当pthread_create成功返回时,由tidp指向的内存单元被设置为新创建线程的线程ID。
attr:一般设为NULL。attr参数用于定制各种不同的线程属性,暂可以把它设置为NULL,以创建默认属性的线程。
(*start_rtn)(void *) :新创建的线程的入口地址。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg。
arg:向start_rtn函数传递的参数。如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg参数传入。

返回值

返回:若成功返回0,否则返回错误编号

2、线程的退出

单个线程可以通过以下三种方式退出,在不终止整个进程的情况下停止它的控制流:
1)线程只是从启动例程中返回,返回值是线程的退出码。
2)线程可以被同一进程中的其他线程取消。
3)线程调用pthread_exit:

函数原型
int pthread_exit(void *rval_ptr);
函数参数

rval_ptr:是一个无类型指针,与传给启动例程的单个参数类似。进程中的其他线程可以通过调用pthread_join函数访问到这个指针。

3、线程的等待

调用这个函数的线程将一直阻塞,直到指定的线程调用pthread_exit、从启动例程中返回或者被取消。

函数原型
int pthread_join(pthread_t thread, void **rval_ptr);
函数参数

thread:线程名
**rval_ptr:线程的返回值。如果对线程的返回值不感兴趣,可以把rval_ptr置为NULL。在这种情况下,调用pthread_join函数将等待指定的线程终止,但并不获得线程的终止状态。

如果例程只是从它的启动例程返回i,rval_ptr将包含返回码。如果线程被取消,由rval_ptr指定的内存单元就置为PTHREAD_CANCELED。
可以通过调用pthread_join自动把线程置于分离状态,这样资源就可以恢复。如果线程已经处于分离状态,pthread_join调用就会失败,返回EINVAL。

返回值

返回:若成功返回0,否则返回错误编号

其他API

线程脱离
函数原型
int pthread_detach(pthread_t thread);
函数参数

一个线程或者是可汇合(joinable,默认值),或者是脱离的(detached)。当一个可汇合的线程终止时,它的线程ID和退出状态将留存到另一个线程对它调用pthread_join。脱离的线程却像守护进程,当它们终止时,所有相关的资源都被释放,我们不能等待它们终止。如果一个线程需要知道另一线程什么时候终止,那就最好保持第二个线程的可汇合状态。
pthread_detach函数把指定的线程转变为脱离状态。

返回值

返回:若成功返回0,否则返回错误编号

本函数通常由想让自己脱离的线程使用,就如以下语句:
pthread_detach(pthread_self());
线程ID获取
函数原型
pthread_t pthread_self(void);
返回值

返回:调用线程的ID

线程ID比较

对于线程ID比较,为了可移植操作,我们不能简单地把线程ID当作整数来处理,因为不同系统对线程ID的定义可能不一样。我们应该要用下边的函数:

函数原型
int pthread_equal(pthread_t tid1, pthread_t tid2);
函数参数

tid1:线程1的名字
tid2:线程1的名字

返回值

返回:若相等则返回非0值,否则返回0

实例一:线程返回打印数字(创建、退出、等待、ID获取)

#include <stdio.h>
#include <pthread.h>
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
//int pthread_join(pthread_t thread, void **retval);
//void pthread_exit(void *retval);
void *func1(void *arg)
{
        static int ret =10;
        static char *p="t1 is run out";
        printf("t1:this thread t1:%ld \n",(unsigned long)pthread_self());
        printf("t1:parm=%d\n",*((int *)arg));
        pthread_exit((void*)&ret);
}
int main()
{
        int ret=0;
        int param =99;
        int *pret = NULL;
        pthread_t t1;
        pthread_create(&t1,NULL,func1,(void*)&param);
        if(ret==0){
                printf("main:create thread success\n");
        }
        printf("main:this thread main:%ld\n",(unsigned long)pthread_self());
        pthread_join(t1,(void **)&pret);
        printf("main:ti quit:%d\n",*pret);
        return 0;
}
实验结果:

在这里插入图片描述

实例一:线程返回打印字符串(创建、退出、等待、ID获取)

#include <stdio.h>
#include <pthread.h>
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
//int pthread_join(pthread_t thread, void **retval);
//void pthread_exit(void *retval);
void *func1(void *arg)
{
        static int ret =10;
        static char *p="t1 is run out";
        printf("t1:this thread t1:%ld \n",(unsigned long)pthread_self());
        printf("t1:parm=%d\n",*((int *)arg));
        pthread_exit((void*)p);
}
int main()
{
        int ret=0;
        int param =99;
        //int *pret = NULL;
        char *pret =NULL;
        pthread_t t1;
        pthread_create(&t1,NULL,func1,(void*)&param);
        if(ret==0){
                printf("main:create thread success\n");
        }
        printf("main:this thread main:%ld\n",(unsigned long)pthread_self());
        pthread_join(t1,(void **)&pret);
        printf("main:ti quit:%s\n",pret);
        return 0;
}
实验结果:

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值