/****************************************************************************************
* 文件名: 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;
}