进程博客:https://blog.csdn.net/lz201788/article/details/79679380
线程
在一个程序里的一个执行路线就叫做线程(thread)。更准确的定义是:线程是一个进程内部的控制序列。一切进程至少都有一个执行线程。
进程与线程:
1.进程是资源竞争的基本单位。
2.线程是程序执行的最小单位。
3.线程共享进程数据,但也拥有自己的一部分数据。
4.Linux下的线程,我们称之为轻量级进程(LWP)。
线程的优点:
1.创建一个新线程的代价要比创建一个进程要小很多。
2.与进程之间的切换相比,线程之间的切换需要操作系统做的工作要少很多。
3.线程占用的资源要比进程少很多。
4.能充分利用多处理器的可并行数量。
5.在等待慢速I/O操作结束的同时,程序可执行其它的计算任务。
6.I/O密集型应用,为了提高性能,将I/O操作重叠,线程可以同时等待不同的I/O操作。
7.计算密集型应用,为了能在多处理器系统上运行,将计算分解到多个线程中实现。
线程的缺点:
1.性能损失
2.健壮性降低
3.缺乏访问控制
4.编程难度提高
线程控制
1.创建线程
#include<pthread.h>
int pthread_create(pthread_t* thread,const pthread_attr_t* attr,void*(*start_routine)(void*),void* arg);
//成功返回0,失败返回错误码
参数:
thread:返回线程ID
attr:设置线程的属性,attr为NULL表示使用默认属性。
start_routine:是函数地址,线程启动后要执行的函数。
arg:传给线程启动函数的参数。
查看线程的指令:ps -eLf | grep a.out | grep -v grep
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
void* rout(void* arg)
{
int i;
for( ; ; )
{
printf("I am thread1\n");
sleep(1);
}
}
int main()
{
pthread_t tid;
int ret;
if((ret=pthread_create(&tid,NULL,rout,NULL))!=0)
{
printf("pthread_create: %s\n",strerror(ret));
exit(1);
}
int i;
for( ; ; )
{
printf("I am main thread\n");
sleep(1);
}
}
2.线程终止:
常见的终止线程的三种方式:
(1)从线程函数return,这种方法对主线程不适用,从main函数return相当于调用exit。
(2)线程可调用pthread_exit终止自己。
(3)一个线程可以调用pthread_cancel终止同一进程中的另一个线程。
注:
pthread_cancel函数,其功能是取消一个执行中的线程。
原型:int pthread_cancel(pthread_t thread)
成功返回0,失败返回错误码。
3.线程等待:
#include<pthread.h>
int pthread_join(pthread_t thread,void **value_ptr);
//thread:线程ID
//value_ptr:它指向一个指针,后者指向线程的返回值
//成功返回0,失败返回错误码
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
void* thread1(void* arg)
{
printf("thread 1 returning ...\n");
int* p=(int*)malloc(sizeof(int));
*p=1;
return (void*)p;
}
void* thread2(void* arg)
{
printf("thread 2 returning ...\n");
int* p=(int*)malloc(sizeof(int));
*p=2;
pthread_exit((void*)p);
}
void* thread3(void* arg)
{
while(1)
{
printf("thread 3 returning ...\n");
sleep(1);
}
return NULL;
}
int main()
{
pthread_t tid;
void* ret;
//thread 1 return
pthread_create(&tid,NULL,thread1,NULL);
pthread_join(tid,&ret);
printf("thread return,thread id %x,return code:%d\n",tid,*(int*)ret);
free(ret);
//thread 2 exit
pthread_create(&tid,NULL,thread2,NULL);
pthread_join(tid,&ret);
printf("thread return,thread id %x,return code:%d\n",tid,*(int*)ret);
free(ret);
//thread 3 cancel by other
pthread_create(&tid,NULL,thread3,NULL);
sleep(3);
pthread_cancel(tid);
pthread_join(tid,&ret);
if(ret==PTHREAD_CANCELED)
{
printf("thread return,thread id %x,return code:PTHREAD_CANCELED\n",tid);
}
else
printf("thread return,thread id %x,return code:NULL\n",tid);
return 0;
}
4.线程分离:
线程的两种状态:(1)可结合的 ; (2)分离状态
(1)默认情况下,新创建的线程是joinable(可结合的),线程退出后,需要对其进行pthread_join操作,否则无法释放资源,从而造成资源泄露。
(2)若不关心线程的返回值,join是一种负担,此时,我们可以告诉系统,当线程退出时,自动释放线程资源。
int pthread_detach(pthread_t thread); //分离线程号为thread的线程
int pthread_detach(pthread_self()); //当前线程将自己分离
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<stdlib.h>
void* thread_run(void* arg)
{
pthread_detach(pthread_self());
printf("%s\n",(char*)arg);
return NULL;
}
int main()
{
pthread_t tid;
if(pthread_create(&tid,NULL,thread_run,"thread run ...")!=0)
{
printf("create thread error!\n");
return 1;
}
int ret=0;
sleep(1);
if(pthread_join(tid,NULL)==0)
{
printf("pthread wait success\n");
ret=0;
}
else
{
printf("pthread wait failed\n");
ret=1;
}
return ret;
}