Linux上的线程相关的API

**

与线程相关的API

**
1、多线程开发在 Linux 平台上已经有成熟的 pthread 库支持。其涉及的多线程开发的最基本概念主要包含三点:线程,互斥锁,条件。其中,线程操作又分线程的创建,退出,等待 3 种。
2、互斥锁则包括 4 种操作,分别是创建,销毁,加锁和解锁。
3、条件操作有 5 种操作:创建,销毁,触发,广播和等待。其他的一些线程扩展概念,如信号灯等,都可以通过上面的三个基本元素的基本操作封装出来。如下表:
在这里插入图片描述
 **

1. 线程创建

**

#include <pthread.h>
int pthread_create(pthread_t *tid, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号

参数:
pthread_t:当前Linux中可理解为:typedef unsigned long int pthread_t;
pthread_t tid:线程id的类型为pthread_t,通常为无符号整型,当调用pthread_create成功时,通过tid指针返回。
const pthread_attr_t *attr:指定创建线程的属性,如线程优先级、初始栈大小、是否为守护进程等。可以使用NULL来使用默认值,通常情况下我们都是使用默认值。
void *(*func) (void *):函数指针func,指定当新的线程创建之后,将执行的函数。
void *arg:线程将执行的函数的参数。如果想传递多个参数,请将它们封装在一个结构体中。

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

2、线程等待

#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
// 返回:若成功返回0,否则返回错误编号

pthread_t tid:指定要等待的线程ID
void ** status:如果不为NULL,那么线程的返回值存储在status指向的空间中(这就是为什么status是二级指针的原因!这种才参数也称为“值-结果”参数)。

3、线程ID获取及比较

#include <pthread.h>
pthread_t pthread_self(void);
// 返回:调用线程的ID

static用法:

在C中,static主要定义全局静态变量、定义局部静态变量、定义静态函数。

1、定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量。全局静态变量有以下特点。
a.在全局区分配内存。
b.如果没有初始化,其默认值为0.
c.该变量在本文件内从定义开始到文件结束可见。

2、定义局部静态变量:在局部变量前面加上关键字static,其特点如下:
a.该变量在全局数据区分配内存。
b.它始终驻留在全局数据区,直到程序运行结束。
c. 其作用域为局部作用域,当定义它的函数或语句块结束时,其作用域随之结束。

#include "stdio.h"
#include "pthread.h"
/*
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号
*/
void *func1(void *arg)
{
	static int ret=10;
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int *)arg));
	pthread_exit((void *)&ret);
}
int main()
{
	int ret;
	int param=100;
	pthread_t t1;	
	int *pret=NULL;
	ret=pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret==0){
		printf("main:create t1 success\n");
	}
	printf("main:%ld\n",(unsigned long)pthread_self());

	pthread_join(t1,(void **)&pret);

	printf("main: t1 quit:%d\n",*pret);

	return 0;
}

``在这里插入图片描述

#include "stdio.h"
#include "pthread.h"
/*int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号
*/


void *func1(void *arg)
{
	static int ret=10;
	static char *p="this is tun out";
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int *)arg));
	pthread_exit((void *)p);
}


int main()
{

	int ret;
	int param=100;
	pthread_t t1;	

	char *pret=NULL;

	ret=pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret==0){
		printf("main:create t1 success\n");
	}

	printf("main:%ld\n",(unsigned long)pthread_self());

	pthread_join(t1,(void **)&pret);

	printf("main: t1 quit:%s\n",pret);

	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值