线程的概念:
线程就是进程的若干个执行流
有些情况需要在⼀个进程中同时执 ⾏多个控制流程,这时候线程就派上了⽤场,⽐如实现⼀个图形界⾯的下载软件,⼀⽅⾯需要和⽤ 户交互,等待和处理⽤户的⿏标键盘事件,另⼀⽅⾯又需要同时下载多个⽂件,等待和处理从多个 ⽹络主机发来的数据,这些任务都需要⼀个“等待-处理”的循环,可以⽤多线程实现,⼀个线程专门 负责与⽤户交互,另外⼏个线程每个线程负责和⼀个⽹络主机通信。
在系统内核中其实是不存在线程的,Linux使用进程模拟线程,线程的实现其实就是多个共享数据代码等信息的进程。所以我们把线程也叫做轻量级进程。
进程分配资源的基本单位,线程调度资源的基本单位。
线程中共享的资源:
- 文件描述符表
- 信号处理方式
- 当前工作目录
- uid,gid
线程中独立的资源:
- 线程id(tid)
- 线程的上下文信息,寄存器信息,PC制作,栈指针
- 栈空间
- 信号屏蔽字
- 调度优先级
- 线程私有数据
线程的函数大部分都放在pthread.h的头文件当中,并且在编译的时候我们需要注意的是加上-lpthread选项,这样就会去动态链接动态库。
线程的控制:
1.创建一个线程:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
第一个参数为指向新线程标识符的指针。
第二个参数用来设置线程属性。不想设置的情况下一般置为NULL.
第三个参数是新线程运行函数的起始地址。
最后一个参数是运行函数的参数。
若线程创建成功,则返回0,若线程创建失败,则返回出错编号。
2.线程创建以后,两个线程的pid和ppid都是一样的,操作系统会为之提供一个线程tid,这个tid我们可以通过一个函数获取。
pthread_t pthread_self();
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
void * thread_run(void * arg)
{
while (1)
{
printf("I am shm...mytid:%lu...pid:%d\n",pthread_self(),getpid());
sleep(1);
}
return 0;
}
int main()
{
pthread_t id;
pthread_create(&id,NULL,thread_run,NULL);//这个id其实就是新线程的id
printf("id = %lu\n",id);
while (1)//两个线程的pid和ppid都是一样的,tid不同。
{
printf("I am main...mytid:%lu...pid:%d\n",pthread_self(),getpid());
sleep(3);
}
return 0;
}
3.新的线程结束后,你的主线程也是需要等待的,然后回收新线程的资源及其他的信息。这样就能确保内存不泄露,所以这里使用一个函数pthread_join
来进行等待。
int phtread_join(pthread_t thread,void ** retval);
thread就是被等待的线程号,retval是一个二级指针,用途是用来获取新线程的退出码的。如果我们不关心新线程的退出码的话,直接传NULL,
这个函数是一个线程阻塞的函数,调用它的线程将一直等待到被等待的新线程结束为止,当函数返回时,被等待的线程的资源被回收,
0表示等待成功,失败返回错误码。
4.终止线程的三种方式
(2):线程退出不能调用exit,如何任意一个线程调用了exit或_exit,则整个进程的所有线程都终止。使用pthread_exit函数终止自己的线程。
void pthread_exit(void *retval); 参数是返回的错误码,想要获得这个错误码,可以通过pthred_join来获得。
(3):使用pthread_cancel函数,可以用来取消进程。 自己可以取消,也可以被别人取消,一般是主线程取消新线程。int pthread_cancel(pthread_t thread); 参数为要终止的线程的tid.
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *thread1(void *_val1)
{
printf("thread 1 returning....\n");
sleep(5);
return (void*)1;
}
void *thread2(void *_val2)
{
printf("thread 2 exiting...\n");
pthread_exit((void*)2); //参数是返回的错误码。
}
void *thread3(void *_val3)
{
while (1)
{
printf("pthread 3 is running ,wait for be cancal...\n");
sleep(1);
}
return NULL;
}
int main()
{
pthread_t tid;
void *tret;
//thread 1 return;
pthread_create(&tid,NULL,thread1,NULL);
pthread_join(tid,&tret); //阻塞式等待新线程完成。
printf("thread,return,thread id is:%lu,return code is:%d\n",tid,(int)tret);
//thread 2 exit
pthread_create(&tid,NULL,thread2,NULL);
pthread_join(tid,&tret);
printf("thread exit,thread id is :%lu,return code is :%d\n",tid,(int)tret);
//thread 3 cancel by other
pthread_create(&tid,NULL,thread3,NULL);
sleep(3);
pthread_cancel(tid);//取消子线程thread3
pthread_join(tid,&tret);
printf("thread return,thread id is:%lu,return code is:%d\n",tid,(int)tret);//此时的返回值是常数PTHREAD_CANCELED也就是-1.
return 0;
}
5.线程分离:
int pthread_detach(phtread_t thread);
当我们把新线程设为可分离的时,这个时候主线程不再等待新线程,分离以后,这个时候的新线程是由操作系统来进行考虑的。
不再由主线程来考虑,这主线程中分离,这个时候主线程知道与新线程分离,主线程是不能调用pthread_join来进行等待的。
注:一般情况下新线程是默认可结合的,需要被主线程回收资源和杀死,一个可结合的线程会容易出现类似于僵尸进程的问题。
一般采用pthread_join来等待。否则就会出现主线程无法获取新线程信息,无法回收线程的资源,会造成内存泄露。
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
//验证子线程的分离
void *thread_run(void *_val)
{
pthread_t tid = pthread_self(); //获取新线程id.
// pthread_detach(tid); //子线程中分离。
printf("%s\n",(char*)_val);
return NULL;
}
int main()
{
pthread_t tid;
int tret = pthread_create(&tid,NULL,thread_run,"thread1 run...");
if (tret != 0)
{
printf("create thread error!,info is:%s\n",strerror(tret));
return tret;
}
//wait
int ret = 0;
sleep(1);
pthread_detach(tid); //在主线程中分离。
if (0 == pthread_join(tid,NULL)) //不关心退出码,直接置NULL,如果新线程已经分离,此时等待会失败.
{
printf("pthread wait success!\n");
}
else
{
printf("pthread wait failed!\n");
ret = 1;
}
return ret;
}

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
//验证子线程的分离
void *thread_run(void *_val)
{
pthread_t tid = pthread_self(); //获取新线程id.
// pthread_detach(tid); //子线程中分离。
printf("%s\n",(char*)_val);
return NULL;
}
int main()
{
pthread_t tid;
int tret = pthread_create(&tid,NULL,thread_run,"thread1 run...");
if (tret != 0)
{
printf("create thread error!,info is:%s\n",strerror(tret));
return tret;
}
//wait
int ret = 0;
sleep(1);
pthread_detach(tid); //在主线程中分离。
if (0 == pthread_join(tid,NULL)) //不关心退出码,直接置NULL,如果新线程已经分离,此时等待会失败.
{
printf("pthread wait success!\n");
}
else
{
printf("pthread wait failed!\n");
ret = 1;
}
return ret;
}
线程同步和互斥:
线程的同步和互斥。当我们使用多线程的时候,多个线程对临界资源进行操作,这个时候如果非互斥的,那么这个时候对同一份临界资源进行操作的时候就会出现冲突的时候,比如当你对临界资源操作的时候,可能会中途进行线程的切换,这个时候原本你所读取的状态会随着硬件上下文和pc指针这些东西会保存下来,切换了线程以后,新切换的线程可能会去读取前一次你所读取的临界资源,然后对这份临界资源进行修改,然后这个时候新线程可能会再次切换,切换到你所原来保存的线程中,然后,回复了以前保存的硬件上下文和pc指针这些内容以后,这个时候线程所读取的临界资源的状态等信息还是在没有修改之前的,所以这个时候就会有隐患,造成一些缺点。
⽐如 两个线程都要把某个全局变量增加1,这个操作在某平台需要三条指令完成:
1. 从内存读变量值到寄存器
2. 寄存器的值加1
3. 将寄存器的值写回内存
正因为这三步操作,所以当不同步的时候,第一个线程已经对增量操作了,但是第二个线程读取到的依然是第一个线程增量操作之前的内容。这样就会出现问题,本来应该由1增加到3的,结果变为了由1到2。
所以从上面所说,可以发现多线程很容易发生上述的访问冲突,所以这里操作系统为我们提供了一个机制叫做互斥锁,这个互斥锁,我们可以去想前面所说的进程间通信的信号量,获得锁的线程可以对临界资源进行操作,没有获得锁的资源阻塞等待,二元信号量类似,获得锁的资源可以进行操作,没有获得锁的资源挂起进程放入挂起队列。
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
int count = 0;//全局变量。
void* pthread_run(void *arg)
{
int val = 0;
int i = 0;
while (i < 5000)
{
//这里会出现问题,就是当两个线程进行操作的时候count的+是非原子的。
i++;
val = count;
printf("pthread:%lu,count:%d\n",pthread_self(),count);
count = val + 1;
}
return NULL;
}
int main()
{
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1,NULL,pthread_run,NULL);
pthread_create(&tid2,NULL,pthread_run,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf("count:%d\n",count);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
//加上互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//锁的初始化。
int count = 0;
void* pthread_run(void *arg)
{
int val = 0;
int i = 0;
while (i < 5000)
{
//在临界区加上互斥锁,这样就可解决线程访问冲突的问题了。
pthread_mutex_lock(&mutex);//加锁,保证操作的原子性。
i++;
val = count;
printf("pthread:%lu,count:%d\n",pthread_self(),count);
count = val + 1;
pthread_mutex_unlock(&mutex);//解锁
}
return NULL;
}
int main()
{
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1,NULL,pthread_run,NULL);
pthread_create(&tid2,NULL,pthread_run,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf("count = %d\n",count);
return 0;
}