嵌入式Linux并发程序设计,线程,线程概念、特点、共享/私有资源,创建线程pthread_create()/回收线程pthread_join()/结束线程pthread_exit()

1,线程概念

进程线程
·进程有独立的地址空间
·Linux为每个进程创建task_struct(在内核中创建任务结构体)
·每个进程都参与内核调度,互不影响
·进程在切换时系统开销大(进程执行的时候有时间片,时间片用完了,或进入等待态了,内核就需要去调度,系统要刷新cache和tlb(页表))
·很多操作系统引入了轻量级进程LWP(即线程)
·同一进程中的线程共享相同地址空间
·Linux不区分进程(独占地址空间)、线程(共享相同地址空间),(系统都认为这是一个任务,都会创建task_struct)

2,线程特点

  1. 通常线程指的是共享相同地址空间的多个任务
  2. 使用多线程的好处
    ·大大提高了任务切换的效率
    ·避免了额外的TLB & cache的刷新

3,线程共享资源–私有资源

  1. 一个进程中的多个线程共享以下资源
    ·可执行的指令
    ·静态数据
    ·进程中打开的文件描述符
    ·当前工作目录
    ·用户ID
    ·用户组ID

  2. 每个线程私有的资源包括
    ·线程ID (TID)
    ·PC(程序计数器)和相关寄存器
    ·堆栈
    ·错误号 (errno)
    ·优先级
    ·执行状态和属性

4,Linux线程库–pthread线程库中提供了如下基本操作

4.1,创建线程pthread_create()

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*routine)(void *), void *arg);

  1. 成功返回0,失败时返回错误码
  2. thread 线程对象
  3. attr 线程属性,NULL代表默认属性
  4. routine 线程执行的函数
  5. arg 传递给routine的参数,不需要旳时候传NULL

4.2.1,回收线程pthread_join()

#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

  1. thread 要回收的线程对象
  2. 调用线程阻塞直到thread结束
  3. *retval 接收线程thread的返回值
  4. 返回值:
    成功返回0,失败时返回错误码

4.2.2,回收线程pthread_detach()

#include <pthread.h>
int pthread_detach(pthread_t thread);

  1. 这将该子线程的状态设置为detached,则该线程运行结束后会自动释放所有资源。
    The pthread_detach() function marks the thread identified by thread as detached. When a detached thread terminates, its resources are auto‐matically released back to the system without the need for another
    thread to join with the terminated thread.
    Attempting to detach an already detached thread results in unspecified behavior.

  2. 返回值:
    On success, pthread_detach() returns 0; on error, it returns an error
    number.

pthread_detach(pthread_self())

4.3,结束线程pthread_exit()

#include <pthread.h>
void pthread_exit(void *retval);

  1. 结束当前线程
  2. retval可被其他线程通过pthread_join获取(retval不能是当前线程栈中创建的地址(不能是局部变量的地址),因为线程一结束,线程所有资源会被释放)
  3. 线程私有资源被释放

5,线程示例

char message[32] = “Hello World”;//全局变量存放在静态存储区,可以被所有线程访问
void *thread_func(void *arg);

int main(void) 
{
	pthread_t  a_thread;
	void *result;
	if (pthread_create(&a_thread, NULL, thread_func, NULL) != 0) 
	{
		printf(“fail to pthread_create”);  exit(-1);
	}
	pthread_join(&a_thread, &result);//等待a_thread线程结束,结束后接受返回值
	printf(“result  is  %s\n”, result);
	printf(“message  is  %s\n”, message);
	return 0;
}
void  *thread_func(void *arg) 
{
	sleep(1);
	strcpy(message, “marked  by  thread”);
	pthread_exit(“thank you for waiting for me”);//等价于return “thank you for waiting for me”;
}
$ gcc  -o  test  test.c  -lpthread//链接pthread线程库
$ ./test
thank you for waiting for me
marked by thread

main函数结束,进程就会结束,进程中所有的线程都会结束

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值