线程和互斥锁
•线程就是“轻量级”的进程。
•线程与创建它的进程共享代码段,数据段。
•线程拥有自己独立的栈。
在实际应用中,多个线程往往会访问同一数据或资源,为避免线程之间相互影响,需要引入线程互斥机制,而互斥锁(mutex)互斥机制中的一种。
特别注意,在使用线程函数的时候,编译和连接还必须要加上一个特殊的库pthread,即 gcc –lpthread …
一个进程创建线程之后,当该进程结束,创建的线程也会结束,但是创建的子进程不会结束。
创建线程
函数原形:
int pthread_create(pthread_t thread, const pthread_attr *attr, void * ( * start routine) (void * ), void *arg);
函数功能: 创建线程,在调用的进程中。
所属头文件:
#include <pthread.h>
返回值:
若成功,返回0;若失败,返回错误编码。
参数说明:
*thread:线程的ID,成功调用会保存新线程的ID。
*attr:新线程的属性(attribute),NULL为默认属性。
void * ( * start routine) (void * ):线程的入口函数,指明线程要执行的函数,从该函数处开始执行线程。
*arg:线程入口函数的参数,可以为NULL。
等待线程结束
函数原形:
int pthread_join(pthread_t thread, void **retval);
函数功能: 用于等待线程的结束
所属头文件:
#include <pthread.h>
返回值:
若成功,返回0;若失败,返回错误编码。
参数说明:
thread:需要等待的线程的ID
** retval:如果非空,则把目标线程的状态保存。一般为NULL
退出线程
函数原形:
void pthread_exit(void **retval);
函数功能: 退出所调用的线程
所属头文件:
#include <pthread.h>
返回值: 无返回值
参数说明:
void **retval:保存返回状态。可为NULL
线程初始化互斥锁
函数原形:
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
函数功能:初始化互斥锁
所属头文件:
#include <pthread.h>
返回值:
若成功,返回0;若失败,返回出错编码
参数说明:
mutex:需要初始化的互斥锁指针
attr:属性,一般为NULL默认属性
互斥锁锁住
函数原形:
int pthread_mutex_lock(pthread_mutex_t *mutex);
函数功能: 给指定的互斥锁锁住,如果指定的互斥锁已经锁住了,就阻塞线程,直到互斥锁被解锁。
所属头文件:
#include <pthread.h>
返回值:
若成功,返回0;若失败,返回出错编码
参数说明:
pthread_mutex_t *mutex:要锁住的互斥锁
互斥锁解锁
函数原形:
int pthread_mutex_unlock(pthread_mutex_t *mutex);
函数功能: 给指定的互斥锁解锁
所属头文件:
#include <pthread.h>
返回值:
若成功,返回0;若失败,返回出错编码
参数说明:
pthread_mutex_t *mutex:要解锁的互斥锁
条件变量
•初始化
pthread_cond_t cond_ready = PTHREAD_COND_INITIALIZER;
•等待条件成熟,在等待条件成熟的线程中使用
pthread_cond_wait(&cond_ready, &mut);
•设置条件成熟,告诉等待的那个线程,本线程已经让条件成熟了
pthread_cond_signal(&cond_ready);
例程:
1、线程互斥:
/* 本文件是关于线程互斥的代码 */
//本进程创建两个线程
#include <stdio.h>
#include <pthread.h>
//两个工人合作垒20层墙
//每次一个线程对一个数进行加1操作,直到20,每个线程加10次
pthread_t thread[2];//定义线程数组
int number = 0;
pthread_mutex_t mutex;//定义一个互斥锁
void * thread_worker1() //注意加星号*
{
int i;
printf("I am work1.\n");
for (i = 0; i < 10; i++)
{
//互斥锁保护
pthread_mutex_lock(&mutex);
number++;//垒一层墙,共20层
//互斥锁解锁
pthread_mutex_unlock(&mutex);
printf("work1, number is %d.\n",number);
sleep(1);//休息一会
}
pthread_exit(NULL);//退出线程
}
void * thread_worker2()
{
int i;
printf("I am work2.\n");
for (i = 0; i < 10; i++)
{
//互斥锁保护
pthread_mutex_lock(&mutex);
number++;//垒一层墙,共20层
pthread_mutex_unlock(&mutex);
printf("work2, number is %d.\n",number);
sleep(1);//休息一会
}
pthread_exit(NULL);//退出线程
}
void main()
{
//互斥锁初始化
pthread_mutex_init(&mutex, NULL);
//创建工人1线程
pthread_create(&thread[0], NULL, thread_worker1, NULL);
//创建工人2线程
pthread_create(&thread[1], NULL, thread_worker2, NULL);
//等待工人1线程结束
pthread_join(thread[0], NULL);
//等待工人2线程结束
pthread_join(thread[1], NULL);
}
2、线程同步:
/* 本代码关于 多线程同步 的编程 */
//A同学扫5次地(++5次),B同学再拖地
#include <stdio.h>
#include <pthread.h>
pthread_t thread[2];//定义线程数组
int number = 0;
pthread_mutex_t mutex;//定义一个互斥锁
pthread_cond_t cond_ready = PTHREAD_COND_INITIALIZER;//初始化,条件变量
void * studentA()
{
int i;
for (i = 0; i < 5; i++)
{
pthread_mutex_lock(&mutex);//互斥锁保护
number++;//扫1次地
printf("the number is %d.\n", number);
if (number >= 5)
{
printf("studentA has finished his work.\n");
//通知B同学,使用条件变量
pthread_cond_signal(&cond_ready);
}
pthread_mutex_unlock(&mutex);//互斥锁解锁
sleep(1);//休息1秒
}
pthread_exit(NULL);//退出线程
}
void * studentB()
{
pthread_mutex_lock(&mutex);//互斥锁保护
if (number < 5)
pthread_cond_wait(&cond_ready, &mutex);//等待A同学,使用条件变量
number = 0;//扫完了,就拖地
printf("StudentB has finished his work.\n");
pthread_mutex_unlock(&mutex);//互斥锁解锁
pthread_exit(NULL);//退出线程
}
void main()
{
//互斥锁初始化
pthread_mutex_init(&mutex, NULL);
//创建A同学线程
pthread_create(&thread[0], NULL, studentA, NULL);
//创建B同学线程
pthread_create(&thread[1], NULL, studentB, NULL);
//等待A同学线程结束
pthread_join(thread[0], NULL);
//等待B同学线程结束
pthread_join(thread[1], NULL);
}