Linux线程

1、线程基础

1)每个用户进程有自己的地址空间

2)系统为每个用户进程创建一个task_struct来描述该进程

              该结构体中包含一个指针指向该进程的虚拟地址空间映射表(0 – 4G)

3)task_struct与虚拟地址空间映射表一起用来表示一个进程

4)由于进程的地址空间是私有的,因此在进程间上下切换时,系统开销较大

5)为了减少系统开销增加系统效率,引进轻量级进程,也称为线程

6)在同一个进程中创建的线程共享该进程的地址空间

7)Linux里同样用task_struct来描述一个线程。线程和进程都参与统一的调度。

8)通常线程指的是共享相同地址空间的多个任务

9)一个线程中的多个线程共享以下资源:

a) 可执行的指令

b) 静态数据

c)进程中打开的文件描述符

d)信号处理函数

e)当前工作目录

f)用户ID

g) 用户组ID

10) 线程ID(TID)

11) PC(程序计数器)和相关寄存器

12)堆栈

       局部变量

       返回地址

13)错误号

14)信号掩码和优先级

15)执行状态和属性

 

2. 多线程编程

1、创建线程函数pthread_create()

头文件:#include<pthread.h>

函数原型:int pthread_create(pthread_t *thread,pthread_attr_t *attr, void *(*routine)(void *), void *arg)

函数参数:thread        创建线程的标识符

                attr            线程属性设置,如设置成NULL则为缺省(default)属性

                routine       线程执行的函数

                arg             传递给routine的参数

函数返回值:成功:0

                  失败:返回错误码

 

2、线程退出函数pthread_exit()

注:退出线程需要使用pthread_exit()函数,这个函数属于线程的主动行为。需要注意的是,不能使用exit()函数试图退出线程,因为exit()函数的作用是使当前进程终止,如果某个线程调用了exit()函数,则会使得进程退出,该进程的所有线程都会直接终止。

 

头文件:#include<pthread.h>

函数原型:void pthread_exit(void *retval)

函数参数:retval  线程结束时的返回值,可以通过pthread_join()接收

 

3、等待线程函数pthread_join()

头文件:#include<pthread.h>

函数原型:int pthread_join(pthread_t thread,void **thread_result)

函数参数:thread               等待线程的标识符

                thread_result     用户定义的指针,当不为NULL时用来接收等待线程结束时的返回值,即pthread_exit()函数内的retval值

函数返回值:成功:0

                     失败:返回错误码

 

4、取消线程函数pthread_cancel()

注:我们可以使用pthread_exit()函数使得线程主动结束。实际应用中,我们经常需要让一个线程去结束另一个线程,此时可以使用pthread_cancel()函数来实现这样的功能。

 

头文件:#include<pthread.h>

函数原型:int pthread_cancel(pthread_t thread)

函数参数:thread 需要取消的线程的标识符

函数返回值:成功:0

                     失败:返回错误码

 

示例程序:

 

/*************************************************************************
 @Author: wanghao
 @Created Time : Fri 18 May 2018 06:39:11 AM PDT
 @File Name: pthread.c
 @Description:
 ************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>


char message[32] = "hello world";
char message1[32] = "I am a robot";
char *pstr = "I am a robot!";
void *thread_function(void *arg);


int main(int argc, const char *argv[])
{
int status;
pthread_t a_thread;
void *buf;
if(pthread_create(&a_thread, NULL, thread_function, (void *)message) < 0)
{
perror("fail to create pthread!\n");
exit(-1);
}
printf("wait for pthread to finish!\n");
if(pthread_join(a_thread,&buf))
{
perror("fail to join pthread!\n");
exit(-1);
}
printf("message is now %s\n",message);
printf("I come from pthread: %s\n",(char *)buf);


return 0;
}


void *thread_function(void *arg)
{
printf("thread_function is running, argument is %s\n",(char *)arg);
strcpy(message, "I am a man");
sleep(2);
pthread_exit((void *)pstr);
// pthread_exit(0);
sleep(2);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值