线程
1,什么是线程
- 线程是进程的一个实体,它是程序运行的最小单位。
- 个进程下有很多线程
- 线程共享进程的内存空间
- 进程下的所有线程可以通过全局变量通信
2,为什么学习线程
- 线程是进程的一个实体,它是程序运行的最小单位,它比进程要消耗更少的资源
- 能共享进程的地址空间(堆栈:程序栈)
3,线程由哪些组成
- 指令指针(指向当前被执行的命令)
- 一个栈(函数栈)
- 寄存器的集合(状态寄存器:一部分正在运行的处理器的状态)
- 一个私有的数据区
4,线程的特点
- 线程切换的开销很低
- 线程的通信机制简单(通过全局变量)
- 线程并不是操作系统中内核所提供的,而是由线程库来提供libpthread.a/.so(静态库和动态库)
- 调用线程离不开os的支持
5,创建线程
pthread_create
pthread_create //创建一个新线程
原型:
int pthread_create((pthread_t *thread, pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
用法:#include <pthread.h>
功能:创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。
说明:
thread:线程标识符;
attr:线程属性设置;
start_routine:线程函数的起始地址;
arg:传递给start_routine的参数;
返回值:成功,返回0;出错,返回-1。
注意:线程是进程的一个实体,一旦主进程运行结束,线程就会被回收,主进程不结束,子进程就会一直挂着
//创建一个简单的线程
#include"stdio.h"
#include"pthread.h"
#include"stdlib.h"
void *test(void *arg)
{
printf("subthread\n");
}
int main()
{
pthread_t id;
int ret = pthread_create(&id,NULL,test,NULL);
if(0 != ret)
{
perror("pthread_create error!");
exit(0);
}
while(1);
return 0;
}
pthread库不是Linux系统默认的库,连接时需要使用静态库libpthread.a,所以在线程函数在编译时,需要连接库函数,如: gcc pthread_create.c -o pthread_create -lpthread
gcc xc.c -o xc -lpthread
//主线程副线程分别做一个输出
#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
void *test()
{
while (1)
{
printf("Testing1\n");
sleep(1);
}
}
int main()
{
pthread_t thread;
int ret = pthread_create(&thread, NULL, test, NULL);
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
while(1)
{
printf("Testing2\n");
sleep(1);
}
pause();
return 0;
}
编译运行结果:
#include"stdio.h"
#include"pthread.h"
#include"stdlib.h"
#include"unistd.h"
struct demo
{
int id;
char name[1024];
};
void *test(void *arg)
{
struct demo *ptr =(struct demo *)arg;
printf("main tread id:%d main_thread_name:%s\n",ptr->id,ptr->name);
sleep(1);
}
int main()
{
struct demo demo1 = {2021,"zhangsan"};
pthread_t id;
int ret = pthread_create(&id,NULL,test,(void *) &demo1);
if(0 != ret)
{
perror("pthread_create error!");
exit(0);
}
while(1)
{
printf("main thread!\n");
sleep(1);
}
pause();
return 0;
}
编译运行结果:
6,线程等待
pthread_join
int pthread_join(pthread_t thread, void **retval);
需要等待的线程 返回状态
#include"stdio.h"
#include"pthread.h"
#include"stdlib.h"
#include"unistd.h"
struct demo
{
int id;
char name[1024];
};
void *test(void *arg)
{
struct demo *ptr =(struct demo *)arg;
ptr->id=1;
printf("main tread id:%d main_thread_name:%s\n",ptr->id,ptr->name);
sleep(1);
}
int main()
{
struct demo demo1 = {2021,"zhangsan"};
int i=4;
pthread_t id;
int ret = pthread_create(&id,NULL,test,(void *) &demo1);
if(0 != ret)
{
perror("pthread_create error!");
exit(0);
}
pthread_join(id, NULL);
printf("main thread!\n");
sleep(1);
return 0;
}
eg:根据输入的内容进行输出
#include"stdio.h"
#include"pthread.h"
#include"unistd.h"
#include"stdlib.h"
#include"string.h"
#define MAX_SIZE 1024
char buffer[MAX_SIZE];
void *thread1(void *arg)
{
while(1)
{
memset(buffer,0,sizeof(buffer));
scanf("%s",buffer);
sleep(1);
}
}
void *thread2(void *arg)
{
while(1)
{
printf("buffer=%s\n",buffer);
sleep(2);
}
}
int main()
{
pthread_t id;
if(pthread_create(&id,NULL,thread1,NULL) !=0)
{
perror("pthread creadte error!\n");
exit(1);
}
pthread_t id2;
if(pthread_create(&id,NULL,thread2,NULL) !=0)
{
perror("pthread creadte error!\n");
exit(1);
}
pause();
return 0;
}
eg:创建两个次线程,两个次线程分别的向同一个文件中写“hello”,“world\n”,和“hhhhhhwwwww\n”
#include"stdio.h"
#include"pthread.h"
#include"unistd.h"
#include"stdlib.h"
#include"string.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"fcntl.h"
#define MAX_SIZE 1024
char buffer[MAX_SIZE];
void *thread1(void *arg)
{
int fd =*((int *)arg);
while(1)
{
write(fd,"hello",5);
write(fd,"world\n",6);
}
}
void *thread2(void *arg)
{
int fd =*((int *)arg);
while(1)
{
write(fd,"hhhhh",5);
write(fd,"wwwww\n",6);
}
}
int main()
{
pthread_t id;
int num =5;
int fd = open("./a.txt",O_RDWR|O_CREAT|O_APPEND,0644);
if(fd <0)
{
perror("open file error");
exit(1);
}
if(pthread_create(&id,NULL,thread1,(void*)(&fd)) !=0)
{
perror("pthread creadte error!\n");
exit(1);
}
pthread_t id2;
if(pthread_create(&id,NULL,thread2,(void*)(&fd)) !=0) {
perror("pthread creadte error!\n");
exit(1);
}
pause();
return 0;
}
7,线程退出
pthread_cancel
被动退出:int pthread_cancel(phread_thread)
-
功能:当次线程是死循环时,可以调动这个函数主动取消该线程。
-
返回值:成功返回0,失败返回非零错误号。
-
参数:thread:要取消线程的TLD。当不发生系统调用的时候线程不会结束
#include"stdio.h"
#include"pthread.h"
#include"unistd.h"
#include"stdlib.h"
#include"string.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"fcntl.h"
void *thread1(void *arg)
{
while(1)
{
printf("helloworld\n");
sleep(1);
}
}
int main()
{
pthread_t id;
if(pthread_create(&id,NULL,thread1,NULL) !=0)
{
perror("pthread creadte error!\n");
exit(1);
}
pthread_cancel(id);
pause();
return 0;
}