1.使用多线程需要包含头文件
#include <pthread.h>
2.创建线程执行的函数
void *func(void *arg)// *arg传参
{
}
3.创建线程
#include <pthread.h>
int pthread_create(pthread_t * tidp, const pthread_attr_t *attr, void *(*start_rtn)(void *), void *arg);
tid : 线程tid
attr: 线程的属性,NULL 使用默认的属性,如下可构造一个属性结构体
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
srart_rtn :线程执行的主题函数
arg :外部传给srart_rtn的参数
注意:attr使用完后记得执行 pthread_attr_destroy(&attr);
4.线程退出
4.1线程执行完后隐式退出
4.2由线程本显示调用pthread_exit函数退出
pthread_exit(void *retval)
4.3其他程序调用pthtead_cancel(指定pid)
pthread_canel(pthread_t thread)
5.一个线程等待另一个线程退出
吐过一个线程要等待另一个线程的终止,可以使用pthread_join(),该函数的作用是调用pthread_join的线程将被挂起直到等待的线程终止退出
6.线程互斥
int aaa;//多线程共享的全局变量
pthread_mutex_t mutex;//互斥量,这个应该也是全局变量
pthread_mutex_init(&mutex,NULL);//按默认属性初始化互斥变量
//对全局变量aaa上锁
pthread_mutex_lock(&mutex);
...对aaa的操作
pthread_mutex_unlock(&mutex);
7.线程同步
同步是指一个线程等待某件事情的发生,只有当等待的事件发生了线程才继续执行,否则线程一直挂起并放弃CPU,
pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *attr);
等待条件发生
pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
广播条件已经发生
pthread_cond_broadcast (pthread_cond_t *cond) ;
解除某个等待线程
pthread_cond_signal (pthread_cond_t *cond) ;
释放条件变量资源
pthread_cond_destroy
例子
pthread_cond_t cond_audio_start;//
pthread_mutex_t audio_mutex;//
pthread_mutex_init(&audio_mutex, NULL);//初始化
pthread_cond_init(&cond_audio_start, NULL);//初始化
//等待条件发生
pthread_cond_wait(&cond_audio_start, &audio_mutex);
pthread_mutex_unlock(&audio_mutex);//释放
//使用完 回收资源
pthread_mutex_destroy(&audio_mutex);
pthread_cond_destroy(&cond_audio_start);
下面我们还是以名的生产者/消费者问题为例来阐述Linux线程的控制和通信。一组生产者线程与一组消费者线程通过缓冲区发生联系。生产者线程将生产的产品送入缓冲区,消费者线程则从中取出产品。缓冲区有N 个,是一个环形的缓冲池。
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 16 // 缓冲区数量
struct prodcons
{
// 缓冲区相关数据结构
int buffer[BUFFER_SIZE]; /* 实际数据存放的数组*/
pthread_mutex_t lock; /* 互斥体lock 用于对缓冲区的互斥操作 */
int readpos, writepos; /* 读写指针*/
pthread_cond_t notempty; /* 缓冲区非空的条件变量 */
pthread_cond_t notfull; /* 缓冲区未满的条件变量 */
};
/* 初始化缓冲区结构 */
void init(struct prodcons *b)
{
pthread_mutex_init(&b->lock, NULL);
pthread_cond_init(&b->notempty, NULL);
pthread_cond_init(&b->notfull, NULL);
b->readpos = 0;
b->writepos = 0;
}
/* 将产品放入缓冲区,这里是存入一个整数*/
void put(struct prodcons *b, int data)
{
pthread_mutex_lock(&b->lock);
/* 等待缓冲区未满*/
if ((b->writepos + 1) % BUFFER_SIZE == b->readpos)
{
pthread_cond_wait(&b->notfull, &b->lock);
}
/* 写数据,并移动指针 */
b->buffer[b->writepos] = data;
b->writepos++;
if (b->writepos >= BUFFER_SIZE)
b->writepos = 0;
/* 设置缓冲区非空的条件变量*/
pthread_cond_signal(&b->notempty);
pthread_mutex_unlock(&b->lock);
}
/* 从缓冲区中取出整数*/
int get(struct prodcons *b)
{
int data;
pthread_mutex_lock(&b->lock);
/* 等待缓冲区非空*/
if (b->writepos == b->readpos)
{
pthread_cond_wait(&b->notempty, &b->lock);
}
/* 读数据,移动读指针*/
data = b->buffer[b->readpos];
b->readpos++;
if (b->readpos >= BUFFER_SIZE)
b->readpos = 0;
/* 设置缓冲区未满的条件变量*/
pthread_cond_signal(&b->notfull);
pthread_mutex_unlock(&b->lock);
return data;
}
/* 测试:生产者线程将1 到10000 的整数送入缓冲区,消费者线
程从缓冲区中获取整数,两者都打印信息*/
#define OVER ( - 1)
struct prodcons buffer;
void *producer(void *data)
{
int n;
for (n = 0; n < 10000; n++)
{
printf("%d --->\n", n);
put(&buffer, n);
} put(&buffer, OVER);
return NULL;
}
void *consumer(void *data)
{
int d;
while (1)
{
d = get(&buffer);
if (d == OVER)
break;
printf("--->%d \n", d);
}
return NULL;
}
int main(void)
{
pthread_t th_a, th_b;
void *retval;
init(&buffer);
/* 创建生产者和消费者线程*/
pthread_create(&th_a, NULL, producer, 0);
pthread_create(&th_b, NULL, consumer, 0);
/* 等待两个线程结束*/
pthread_join(th_a, &retval);
pthread_join(th_b, &retval);
return 0;
}
注意:编译是加-lpthread 选项