arm linux线程退出,【ARM&Linux】linux系统下多线程编程

本文详细介绍了在Unix环境下进行多线程编程的控制方法,包括线程创建、等待线程结束及线程退出。同时,通过一个扫雪任务的模拟案例,展示了如何使用互斥锁实现线程间的资源同步,确保了两个线程在只有一个扫把的情况下轮流扫雪,从而避免了并发冲突。
摘要由CSDN通过智能技术生成

/****************************************************************************************

* 文件名: pro.c

* 创建者:

* 时 间:

* 联 系:

* 简 介: 多线程编程控制,参考API文档《Unix环境高级编程》

*****************************************************************************************/

/***********************************************************

函数学习:

1、创建线程: int pthread_create(pthread_t *thread,

const pthread_attr_t *attr,

void *(*start_routine) (void *),

void *arg);

返回值: 成功0 失败返回错误编码

参数一:线程id

参数二:存储线程的属性的结构

参数三:指明线程的入口函数

参数四:入口函数的参数,可为NULL

2、等待线程结束: int pthread_join(pthread_t thread, void **retval);

参数一:要等待结束的线程id

参数二:保存线程退出的状态,一般为NULL

返回值:成功0,否则返回错误代码

3、线程退出: void pthread_exit(void *retval); 结束调用此函数的线程

参数一:保存返回值

-------------------------------------------------------------------

线程互斥:

【互斥锁】:

1、初始化:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);

参数一:根据参数二的 属性初始化互斥锁

参数二:互斥锁的属性,如果为NULL,使用默认属性

返回值:成功0,否则返回错误代码

2、获取: int pthread_mutex_lock(pthread_mutex_t *mutex);

参数一: 指明要锁住的互斥锁

返回值: 成功0,否则返回错误代码

3、释放锁: int pthread_mutex_unlock(pthread_mutex_t *mutex);

参数一: 指明要解开的互斥锁

返回值: 成功0,否则返回错误代码

4、注销互斥锁: int pthread_mutex_destroy(pthread_mutex_t *mutex);

参数一:要删除的互斥锁

返回值: 成功0,否则返回错误代码

-----------------------------------------------------------------------

【综合实例】:

一位老师让学生去扫雪,老师充当主进程,学生充当线程2,班长充当线程1,两个人需要扫20此才能够扫完一条小路。

但是目前扫把只有一个,只有在其中一个人休息的时候另一个能才能扫雪,所以扫把是共享资源。

************************************************************/

#include "mytype.h"

#define PTHREAD_COUNT 2

static unsigned int clean_snow_cnt = 0;

pthread_mutex_t mulock; //互斥锁

void delay()

{

int i,k;

for(i=0; i<10000; i++)

for(k=0; k<10000; k++)

;

}

void *stu1_opertion()

{

int cnt;

int res;

DEBUG_INFO("I am stu1 .

");

for(cnt=0; cnt<10; cnt++)

{

pthread_mutex_lock(&mulock); //加锁

++clean_snow_cnt;

pthread_mutex_unlock(&mulock); //释放互斥锁

DEBUG_INFO("[stu1]-> clean snow count = %d

", clean_snow_cnt);

sleep(1);

}

DEBUG_INFO("student 1 clean snow end .

");

//线程退出

pthread_exit(&res);

if(res != 0)

{

DEBUG_ERROR("pthread_exit-> stu1 failed .

");

}

return 0;

}

void *stu2_opertion()

{

int cnt;

int res;

DEBUG_INFO("I am stu2 .

");

for(cnt=0; cnt<10; cnt++)

{

pthread_mutex_lock(&mulock); //加锁

++clean_snow_cnt;

pthread_mutex_unlock(&mulock); //释放互斥锁

DEBUG_INFO("[stu2]-> clean snow count = %d

", clean_snow_cnt);

sleep(1);

}

DEBUG_INFO("student 2 clean snow end .

");

//线程退出

pthread_exit(&res);

if(res != 0)

{

DEBUG_ERROR("pthread_exit-> stu2 failed .

");

}

return 0;

}

int main()

{

int res; //保存返回值

int count;

pthread_t stu_thread[PTHREAD_COUNT];

// 初始化互斥锁

res = pthread_mutex_init(&mulock,NULL);

if(res != 0)

{

DEBUG_ERROR("mutex lock init failed .

");

exit(EXIT_FAILURE);

}

// 1、创建学生线程

if( (res=pthread_create(&stu_thread[0], NULL, stu1_opertion, NULL)) ==0 )

{

DEBUG_INFO("creat stu1 successful .

");

}

// 2、创建学生线程2

if( (res=pthread_create(&stu_thread[1], NULL, stu2_opertion, NULL)) == 0)

{

DEBUG_INFO("creat stu2 successful .

");

}

// 3.等待学生线程扫雪结束

for(count=0; countif(res != 0)

{

DEBUG_WARNING("pthread_join-> stu %d failed , error code : %d

", count+1, res);

}

}

// 删除互斥锁

res = pthread_mutex_destroy(&mulock);

if(res != 0)

{

DEBUG_ERROR("pthread_mutex_destroy failed, error code : %d", res);

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值