达者为先 师者之意
1 什么是线程
线程
什么是线程?
在一个程序里的一个执行路线就叫做线程(thread)。更准确的定义是:线程是“一个进程内部的控制序列”。
一切进程至少都有一个执行线程。
线程是进程内部的一条执行序列或执行路径,即一个可调度的实体。一个进程可以包含多条线程。
进程— 资源分配的最小单位
线程— 程序执行的最小单位
进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其他进程产生影响,而线程只是一个进程中的不同执行路径。线程有自己的堆栈和局部变量,但线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些,但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。
进程是一个程序的一个实例,拥有自己独立的各种段(数据段,代码段等等),每次创建一个进程需要从操作系统分配这些资源给他,消耗一定的时间,在linux下C语言创建一个进程使用fork()函数;
线程是一个轻量级的进程,除了自己少数的资源,不用用其他资源,且一个进程可以创建多个线程,这些线程共享进程的资源,创建线程的时间要比创建进程少很多,(几十分之一),从函数角度是使用clone()创建。
使用线程处理文件I/O或者socket处理都是非常有优势的,将一个大人物分解成若干个小任务,每个线程处理一个任务,线程之间切换不需要花很多时间,而且线程之间数据交换很方便,共享存储区。
总的来说就是:进程有独立的地址空间,线程没有单独的地址空间(同一进程内的线程共享进程的地址空间)。
使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式。我们知道,在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种"昂贵"的多任务工作方式。而运行于一个进程中的多个线程,它们彼此之间使用相同的地址空间,共享大部分数据,启动一个线程所花费的空间远远小于启动一个进程所花费的空间,而且,线程间彼此切换所需的时间也远远小于进程间切换所需要的时间。据统计,总的说来,一个进程的开销大约是一个线程开销的30倍左右,当然,在具体的系统上,这个数据可能会有较大的区别。
使用多线程的理由之二是线程间方便的通信机制。对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过通信的方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。当然,数据的共享也带来其他一些问题,有的变量不能同时被两个线程所修改,有的子程序中声明为static的数据更有可能给多线程程序带来灾难性的打击,这些正是编写多线程程序时最需要注意的地方。
除了以上所说的优点外,不和进程比较,多线程程序作为一种多任务、并发的工作方式,当然有以下的优点:
- 提高应用程序响应。这对图形界面的程序尤其有意义,当一个操作耗时很长时,整个系统都会等 待这个操作,此时程序不会响应键盘、鼠标、菜单的操作,而使用多线程技术,将耗时长的操作(time consuming)置于一个新的线程,可以避免这种尴尬的情况。
- 使多CPU系统更加有效。操作系统会保证当线程数不大于CPU数目时,不同的线程运行于不同的CPU上。
- 改善程序结构。一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。
2 创建线程
函数原型
#include <pthread.h>
int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, void *(*start_rtn)(void*),void *arg);
其返回值是一个整数,若创建进程成功返回0,否则,返回其他错误代码,也是正整数。
创建线程需要的参数:
- 返回值:成功返回0,否则返回出错编号,并且thread中的内容是未定义的。
- pthread_t *tidp:线程id的类型为pthread_t,通常为无符号整型,调用pthread_create成功时,通过tid指针返回。
- pthread_attr_t *attr:指定创建线程的属性,如线程优先级、初始栈大小、是否为守护进程等。可以使用NULL来使用默认值,通常情况下我们都是使用默认值。
- void *(start_rtn)(void):函数指针start_rtn,指定当新的线程创建之后,将执行的函数。
- void *arg:线程将执行的函数的参数。如果想传递多个参数,请将它们封装在一个结构体中。
当 pthread_create成功返回时,
由tidp指向的内存单元被设置为新创建线程的线程ID。
attr参数用于定制各种不同的线程属性,暂可以把它设置为NULL,以创建默认属性的线程。
新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg。
如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg参数传入。
注意:在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库。
3 结束线程
函数原型:
#include <pthread.h>
void pthread_exit(void* retval);
retval 是一个无类型指针,可以指向任何类型的数据,它指向的数据将作为线程退出时的返回值。如果线程不需要返回任何数据,将 retval 参数置为NULL即可。
实现:用于终止线程,可以指定返回值,以便其他线程通过pthread_join函数获取该线程的返回值。
void* retval:指针线程终止的返回值。
4 线程等待
线程创建后怎么执行,新线程和老线程谁先执行这些不是程序来决定,而是由操作系统进行调度的,但是在编程的时候我们常常需要多个线程配合工作,比如在结束某个线程之前,需要等待另外一个线程的处理结果(返回状态等信息),这时候就需要使用线程等待函数。
函数原型:
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
- 返回值:成功返回0,否则返回出错编号。
- pthread_t thread:表示要等待的进程的id;
- void ** retval:表示要等待的进程的返回状态,是个二级指针,如果不为NULL,那么线程的返回值存储在retval指向的空间中(这种参数也称为“值-结果”参数)。
调用这个函数的线程将一直阻塞,直到指定的线程调用pthread_exit、从启动例程中返回或者被取消。如果例程只是从它的启动例程返回i,rval_ptr将包含返回码。如果线程被取消,由retval指定的内存单元就置为PTHREAD_CANCELED。
可以通过调用pthread_join自动把线程置于分离状态,这样资源就可以恢复。如果线程已经处于分离状态,pthread_join调用就会失败,返回EINVAL。
如果对线程的返回值不感兴趣,可以把retval置为NULL。在这种情况下,调用pthread_join函数将等待指定的线程终止,但并不获得线程的终止状态。
返回当前线程ID
#include <pthread.h>
pthread_t pthread_self (void);
// 返回:调用线程的ID
对于线程ID比较,为了可移植操作,我们不能简单地把线程ID当作整数来处理,因为不同系统对线程ID的定义可能不一样。我们应该要用下边的函数:
#include <pthread.h>
int pthread_equal(pthread_t tid1, pthread_t tid2);
// 返回:若相等则返回非0值,否则返回0
代码例子
#include <stdio.h>
#include <pthread.h>
void *func1(void *arg)
{
static int ret = 13;
printf("t1:the id of the pthread is:%ld\n",(unsigned long)pthread_self());
printf("the arg is:%d\n",*((int *)arg));
pthread_exit((void *)&ret);
}
int main()
{
pthread_t t1;
int ret;
int param = 12;
int *pret = NULL;
ret = pthread_create(&t1,NULL,func1,(int *)¶m);
if(ret == 0)
{
printf("Success to create the t1 pthread\n");
}
printf("main:the id:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,(void **)&pret);
printf("the pret is:%d\n",*pret);
return 0;
}
Success to create the t1 pthread
main:the id:139775874971392
t1:the id of the pthread is:139775866685184
the arg is:12
the pret is:13
用两个或两个以上的线程验证同一进程下的线程是共用内存空间的,且这时候的线程是随机输出的,即没有顺序的
代码例子
#include <stdio.h>
#include <pthread.h>
int r_data = 0;
void *func1(void *arg)
{
printf("t1:the id of the pthread is:%ld\n",(unsigned long)pthread_self());
printf("t1:the arg is:%d\n",*((int *)arg));
while(1)
{
printf("t1:the r_data is:%d\n",r_data++);
sleep(1);
}
pthread_exit(NULL);
}
void *func2(void *arg)
{
printf("t2:the id of the pthread is:%ld\n",(unsigned long)pthread_self());
printf("t2:the arg is:%d\n",*((int *)arg));
while(1)
{
printf("t2:the r_data is:%d\n",r_data++);
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
pthread_t t1;
pthread_t t2;
int ret1;
int ret2;
int param = 12;
ret1 = pthread_create(&t1,NULL,func1,(int *)¶m);
if(ret1 == 0)
{
printf("Success to create the t1 pthread\n");
}
ret2 = pthread_create(&t2,NULL,func2,(int *)¶m);
if(ret2 == 0)
{
printf("Success to create the t2 pthread\n");
}
while(1)
{
printf("main:the r_data is:%d\n",r_data++);
sleep(1);
}
printf("main:the id:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}
Success to create the t1 pthread
t1:the id of the pthread is:140586490631936
t1:the arg is:12
t1:the r_data is:0
Success to create the t2 pthread
main:the r_data is:1
t2:the id of the pthread is:140586482239232
t2:the arg is:12
t2:the r_data is:2
t1:the r_data is:3
main:the r_data is:4
t2:the r_data is:5
5 互斥量与锁
锁机制
多线程之间可能需要互斥的访问一些全局变量,这就需要互斥的来访问,这些需要共享访问的字段被称作是临界资源,访问临界资源的程序段称作是临界区。
实现线程间的互斥与同步机制的是锁机制
互斥量(mutex)从本质上来说是一把锁,在访问共享资源前对互斥量进行加锁,在访问完成后释放互斥量上的锁。对互斥量进行加锁后,任何其他试图再次对互斥量加锁的线程将会被阻塞直到当前线程释放该互斥锁。如果释放互斥锁时有多个线程阻塞,所有在该互斥锁上的阻塞线程都会变成可运行状态,第一个变为可运行状态的线程可以对互斥量加锁,其他线程将会看到互斥锁依然被锁住,只能回去等待它重新变为可用。在这种方式下,每次只有一个线程可以向前运行。
在设计时需要规定所有的线程必须遵守相同的数据访问规则。只有这样,互斥机制才能正常工作。操作系统并不会做数据访问的串行化。如果允许其中的某个线程在没有得到锁的情况下也可以访问共享资源,那么即使其它的线程在使用共享资源前都获取了锁,也还是会出现数据不一致的问题。
互斥变量用pthread_mutex_t数据类型表示。在使用互斥变量前必须对它进行初始化,可以把它置为常量PTHREAD_MUTEX_INITIALIZER(只对静态分配的互斥量),也可以通过调用pthread_mutex_init函数进行初始化。如果动态地分配互斥量(例如通过调用malloc函数),那么在释放内存前需要调用pthread_mutex_destroy。
6 创建锁与销毁锁
创建锁
函数原型
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr)
- 返回值:若成功返回0,否则返回错误编号
- restrict mutex:全局变量,通常定义全局变量为 pthread_mutex_t nutex,调用时取地址传参 &nutex(pthread_mutex_init(&nutex,NULL) )在主线程中初始化锁为解锁状态)
- attr:要用默认的属性初始化互斥量,只需把attr设置为NULL。
除了使用 pthread_mutex_init() 初始化一个互斥锁,我们还可以使用下面的方式定义一个互斥锁:
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
销毁锁
函数原型
#include <pthread.h>
int pthread_mutex_destroy(pthread_mutex_t *mutex);
- 返回值:若成功返回0,否则返回错误编号
- 参数:创建锁时的全局变量,通常定义为 pthread_mutex_t nutex,调用时取地址传参 &nutex
7 加锁及解锁
函数原型
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
//该互斥锁已被锁定。线程调用该函数让互斥锁上锁,
//如果该互斥锁已被另一个线程锁定和拥有,则调用该线程将阻塞,直到该互斥锁变为可用为止
int pthread_mutex_trylock(pthread_mutex_t *mutex);
//函数pthread_mutex_trylock是pthread_mutex_lock的非阻塞版本 //如果mutex参数所指定的互斥锁已经被锁定的话,调用pthread_mutex_trylock函数不会阻塞当前线程,而是立即返回一个值来描述互斥锁的状况。
int pthread_mutex_unlock(pthread_mutex_t *mutex);
- 返回值:若成功返回0,否则返回错误编号
- 参数 : 创建锁时的全局变量,通常定义为 pthread_mutex_t nutex,调用时取地址传参 &nutex
如果线程不希望被阻塞,它可以使用pthread_mutex_trylock尝试对互斥量进行加锁。如果调用pthread_mutex_trylock时互斥量处于未锁住状态,那么pthread_mutex_trylock将锁住互斥量,不会出现阻塞并返回0,否则pthread_mutex_trylock就会失败,不能锁住互斥量,而返回EBUSY。
代码例子
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int data = 0;
pthread_mutex_t nutex;
void *fun1(void *arg)
{
printf("t1:id=%ld\n",(unsigned long)pthread_self());
pthread_mutex_lock(&nutex);
while(1){
data++;
printf("t1:data=%d\n",data);
if(data == 3){
printf("t1 quit =============================\n");
pthread_mutex_unlock(&nutex);
pthread_exit(NULL);
}
sleep(1);
}
}
void *fun2(void *arg)
{
printf("t2:id=%ld\n",(unsigned long)pthread_self());
while(1){
pthread_mutex_lock(&nutex);
data++;
pthread_mutex_unlock(&nutex);
printf("t2:data=%d\n",data);
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
pthread_t t1;
pthread_t t2;
int arg = 100;
pthread_mutex_init(&nutex, NULL);
if(pthread_create(&t1, NULL, fun1, (void *)&arg) == 0){
printf("found thread succeed\n");
}
if(pthread_create(&t2, NULL, fun2, (void *)&arg) == 0){
printf("found thread succeed\n");
}
printf("main:id=%ld\n",(unsigned long)pthread_self());
while(1){
printf("main:%d\n",data);
sleep(1);
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&nutex);
return 0;
}
found thread succeed
t1:id=140562014590720
t1:data=1
found thread succeed
main:id=140562023094080
main:1
t2:id=140562006198016
main:1
t1:data=2
t1:data=3
t1 quit =============================
main:2
t2:data=4
main:4
t2:data=5
t2:data=6
main:6
注意死锁的情况:
2个线程有时可能会造成死锁,
2个锁一个线程拿到锁1,要拿锁2,
另一个拿到锁2,要拿锁1,
造成2个线程都不能继续运行下去。
代码例子
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int data = 0;
pthread_mutex_t nutex;
pthread_mutex_t nutex2;
void *fun1(void *arg)
{
printf("t1:id=%ld\n",(unsigned long)pthread_self());
pthread_mutex_lock(&nutex);
pthread_mutex_lock(&nutex2);
while(1){
data++;
printf("t1:data=%d\n",data);
if(data == 3){
printf("t1 quit =============================\n");
pthread_mutex_unlock(&nutex);
pthread_exit(NULL);
}
sleep(1);
}
}
void *fun2(void *arg)
{
printf("t2:id=%ld\n",(unsigned long)pthread_self());
while(1){
pthread_mutex_lock(&nutex2);
pthread_mutex_lock(&nutex);
data++;
pthread_mutex_unlock(&nutex);
printf("t2:data=%d\n",data);
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
pthread_t t1;
pthread_t t2;
int arg = 100;
pthread_mutex_init(&nutex, NULL);
pthread_mutex_init(&nutex2, NULL);
if(pthread_create(&t1, NULL, fun1, (void *)&arg) == 0){
printf("found thread succeed\n");
}
if(pthread_create(&t2, NULL, fun2, (void *)&arg) == 0){
printf("found thread succeed\n");
}
printf("main:id=%ld\n",(unsigned long)pthread_self());
while(1){
printf("main:%d\n",data);
sleep(1);
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&nutex);
pthread_mutex_destroy(&nutex2);
return 0;
}
8 与条件变量相关的API
条件变量是线程另一可用的同步机制。条件变量给多个线程提供了一个会合的场所。条件变量与互斥量一起使用时,允许线程以无竞争的方式等待特定的条件发生。
条件本身是由互斥量保护的。线程在改变条件状态前必须首先锁住互斥量,其他线程在获得互斥量之前不会察觉到这种改变,因为必须锁定互斥量以后才能计算条件。
条件变量使用之前必须首先初始化,pthread_cond_t数据类型代表的条件变量可以用两种方式进行初始化,可以把常量PTHREAD_COND_INITIALIZER赋给静态分配的条件变量,但是如果条件变量是动态分配的,可以使用pthread_cond_destroy函数对条件变量进行去除初始化(deinitialize)。
创建及销毁条件变量
#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
//除非需要创建一个非默认属性的条件变量,否则pthread_cont_init函数的attr参数可以设置为NULL
int pthread_cond_destroy(pthread_cond_t *cond);
// 返回:若成功返回0,否则返回错误编号
- restrict cond:全局变量,通常定义为 pthread_cond_t cond;,调用时取地址传参 &cond( pthread_cond_init(&cond,NULL))
- attr:除非需要创建一个非默认属性的条件变量,否则pthread_cont_init函数的attr参数可以设置为NULL
除了使用 pthread_cond_init() 初始化一个互斥锁,我们还可以使用下面的方式初始化条件变量:
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
等待
函数原型:
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_cond_wait(&cond, &mutex);
返回值:若成功返回0,否则返回错误编号
- restrict cond:全局变量,通常定义为 pthread_cond_t cond;,调用时取地址传参 &cond( pthread_cond_init(&cond,NULL))
- restrict mutex : 创建锁时的全局变量,通常定义全局变量为 pthread_mutex_t nutex,调用时取地址传参 &nutex
pthread_cond_wait等待条件变为真。如果在给定的时间内条件不能满足,那么会生成一个代表一个出错码的返回变量。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传给函数。函数把调用线程放到等待条件的线程列表上,然后对互斥量解锁,这两个操作都是原子操作。这样就关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道,这样线程就不会错过条件的任何变化。pthread_cond_wait返回时,互斥量再次被锁住。
pthread_cond_timedwait函数的工作方式与pthread_cond_wait函数类似,只是多了一个timeout。timeout指定了等待的时间,它是通过timespec结构指定。
触发
函数原型:
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
- 返回值:若成功返回0,否则返回错误编号
- restrict cond:全局变量,通常定义为 pthread_cond_t cond;,调用时取地址传参 &cond( pthread_cond_init(&cond,NULL))
这两个函数可以用于通知线程条件已经满足。pthread_cond_signal函数将唤醒等待该条件的某个线程,而pthread_cond_broadcast函数将唤醒等待该条件的所有进程。
注意一定要在改变条件状态以后再给线程发信号。
代码例子
使用了条件变量中的等待与触发来实现当data到达某个值的时候让特定的线程来执行
#include <stdio.h>
#include <pthread.h>
int r_data = 0;
pthread_mutex_t mutex;
pthread_cond_t cond;
void *func1(void *arg)
{
printf("t1:the id of the pthread is:%ld\n",(unsigned long)pthread_self());
printf("t1:the arg is:%d\n",*((int *)arg));
while(1)
{
pthread_cond_wait(&cond,&mutex);
printf("t1:the r_data is:%d\n",r_data);
printf("t1 is run ========!!!!!\n");
r_data = 0;
sleep(1);
}
}
void *func2(void *arg)
{
printf("t2:the id of the pthread is:%ld\n",(unsigned long)pthread_self());
printf("t2:the arg is:%d\n",*((int *)arg));
while(1)
{
printf("t2:the r_data is:%d\n",r_data);
pthread_mutex_lock(&mutex);
r_data++;
if(r_data == 3)
{
pthread_cond_signal(&cond);
}
sleep(1);
pthread_mutex_unlock(&mutex);
}
}
int main()
{
pthread_t t1;
pthread_t t2;
int ret1;
int ret2;
int param = 12;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
ret1 = pthread_create(&t1,NULL,func1,(int *)¶m);
if(ret1 == 0)
{
// printf("Success to create the t1 pthread\n");
}
ret2 = pthread_create(&t2,NULL,func2,(int *)¶m);
if(ret2 == 0)
{
// printf("Success to create the t2 pthread\n");
}
/* while(1)
{
printf("main:the r_data is:%d\n",r_data);
sleep(1);
}
*/
printf("main:the id:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
码字不易 求个三连