(1)非阻塞式,代码继续走,能回收就回收,不能回收继续运行程序,但需要判断子程序返回信号,缺点是cpu占有率高。
进程概念比线程大一些,但是都能并发
thread线程:优点:比多进程节省资源,可以共享变量。
新线程创建就会新建栈。
线程是轻量级进程,一般是一个进程中的多个任务。
进程是系统的最小资源分配单位,线程是系统最小执行单位。
线程共享进程资源,除了栈区。进程空间独立
并发程度:线程并发度高一些,线程切换容易,只需要栈区切换。
线程稳定性稍差,
进程可以申请到硬件资源,线程只能用进程的资源。
资源竞争问题
栈区,pc寄存器切换
线程效率高30%,只需要栈区创建回收
三方库:pthread.h :posix协议:便于移植的
编译代码加载库:-lpthread library
动态库(共享库)文件命名:lib***.so
线程稳定性差一些。有一个线程栈区崩溃,整个进程都会结束
资源够用,选线程
线程调试更麻烦
线程资源共享ipc,栈区资源不共享,必须独立出来,
线程的设计框架 posix
创建多线程-》线程空间操作-》线程资源回收
线程是平级的,创建新线程时默认有一个主线程,新创建的是次线程。
(2)创建:int pthread_create(*新线程线程号,属性(NULL默认8m),函数名,*参数)
|-------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> void *th1 (void *arg) { while(1) { printf("发送视频\n"); sleep(1); } } void *th2 (void *arg) { while(1) { printf("接收控制\n"); sleep(1); } } int main(int argc, char **argv) { pthread_t tid1,tid2; pthread_create(&tid1,NULL,th1,NULL); pthread_create(&tid2,NULL,th2,NULL); while(1)sleep(1); //system("pause"); return 0; } |
编译时:gcc main.c -lpthread
sterror(号码)返回错误信息
errno返回错误号,
perror返回错误信息
l都加上
主线程退出,进程就结束了
ps aux中SL+ 这个l表示线程
ps -eLf显示线程
没有0号进程,最早只有1
LWP轻量级进程
主线程号跟进程号一样
伪终端pts/1表示跟哪个终端关联
tty真正终端
tid线程好:用户层表示长一些,linux系统层采用短编号,他们有对应关系。
(3)
pthread_self()获取tid号
pthread_exit(void *retval);子线程自行退出
int pthread_cancel(pthread_t thread)主线程删除子线程。
|----------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> void *th1 (void *arg) { while(1) { printf("发送视频 th1, tid:%ld\n",pthread_self()); sleep(1); } } void *th2 (void *arg) { while(1) { printf("接收控制th2, tid:%ld\n",pthread_self()); sleep(1); } } int main(int argc, char **argv) { pthread_t tid1,tid2; pthread_create(&tid1,NULL,th1,NULL); pthread_create(&tid2,NULL,th2,NULL); while(1) { printf("main th, tid:%ld\n",pthread_self()); sleep(1); } //system("pause"); return 0; } |
|-------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> int main(int argc, char **argv) { int i = 0 ; for(i=0;i<400;i++) { printf("%d %s\n",i,strerror(i)); } //system("pause"); return 0; } |
|----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> void* th(void*arg) { int i =3; while(i--) { printf("th,tid:%ld\n",pthread_self()); sleep(1); } pthread_exit(NULL); //return NULL; } int main(int argc, char **argv) { pthread_t tid; pthread_create(&tid,NULL,th,NULL); while(1)sleep(1); system("pause"); return 0; } |
|----------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> void* th(void*arg) { int i =3; while(i) { printf("th,tid:%ld\n",pthread_self()); sleep(1); } pthread_exit(NULL); //return NULL; } int main(int argc, char **argv) { pthread_t tid; pthread_create(&tid,NULL,th,NULL); int i = 0 ; while(1) { printf("main th, tid %ld\n",pthread_self()); sleep(1); i++; if(3 == i ) { pthread_cancel(tid); } } //system("pause"); return 0; } |
(5)
线程回收:栈区资源回收:int pthread_join(pthread_t thread,void **retval)
有阻塞,等子进程走完回收它的栈区,接住返回值,
pcb里有tcb线程控制块
地址:栈区变量,全局变量,静态变量。堆区变量
|-------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> void * th(void* arg) { int i = 5; while(i--) { printf("th tid:%ld\n",pthread_self()); sleep(1); } return NULL; } int main(int argc, char **argv) { pthread_t tid; pthread_create(&tid,NULL,th,NULL); //while(1)sleep(1); pthread_join(tid,NULL); system("pause"); return 0; } |
|----------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> void * th(void* arg) { //static //char str[]="我要消亡了"; char* p = malloc(20); strcpy(p,"我要消亡了"); sleep(3); return p; } int main(int argc, char **argv) { pthread_t tid; pthread_create(&tid,NULL,th,NULL); //while(1)sleep(1); void* ret; pthread_join(tid,&ret); printf("ret %s\n",(char*)ret);// void* ->char* free(ret); system("pause"); return 0; } |
|----------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> int a = 0 ; void* th(void* arg) { sleep(1); a+=10; return NULL; } int main(int argc, char **argv) { pthread_t tid; printf("a is %d\n",a); pthread_create(&tid,NULL,th,NULL); pthread_join(tid,NULL); printf("a is %d\n",a); system("pause"); return 0; } |
(6)设置分离属性,与主线程分离,目的线程消亡,自动回收空间,不能调用join回收。
int pthread_detach(tid)
|----------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> void* th(void* arg) { pthread_detach(pthread_self()); //栈区系统自动回收 return NULL; } int main(int argc, char** argv) { int i = 0; pthread_t tid; for (i = 0; i < 50000; i++) { int ret = pthread_create(&tid, NULL, th, NULL); if (ret != 0) { break; } printf("%d\n", i); } system("pause"); return 0; } |
(7)注册清理函数:
|-------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> void clean(void *arg) { printf("this clean %s\n", (char*)arg); free(arg); } void* th(void* arg) { strcpy((char* )arg,"hello world\n"); printf("th,strcpy over\n"); return NULL; } int main(int argc, char **argv) { pthread_t tid; char* p = malloc(50); pthread_cleanup_push(clean,p); pthread_create(&tid,NULL,th,p); pthread_join(tid,NULL); printf("before pop\n"); pthread_cleanup_pop(1); system("pause"); return 0; } |
(8)多元素传递
|----------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> typedef struct { char name[50]; int age; char addr[50]; }PER; void* th(void* arg) { PER* per = (PER*)arg; printf("th:%s %d %s\n",per->name,per->age,per->addr); strcat(per->name,"1"); return per; } int main(int argc, char **argv) { PER per; bzero(&per,sizeof(per)); printf("pls name:"); fgets(per.name,sizeof(per.name),stdin);//zhagnsan\n per.name[strlen(per.name)-1]='\0'; char buf[5]={0}; printf("pls age:"); fgets(buf,sizeof(buf),stdin); per.age = atoi(buf);//12345 printf("pls addr:"); fgets(per.addr,sizeof(per.addr),stdin); per.addr[strlen(per.addr)-1]='\0'; pthread_t tid; pthread_create(&tid,NULL,th,&per);// int * -->void* void* ret=NULL; pthread_join(tid,&ret); printf("join ret :%s %d %s\n", ((PER*)ret)->name, ((PER*)ret)->age,((PER*)ret)->addr); system("pause"); return 0; } |