线程的基本操作

发布时间:2020-07-05 23:20:45 作者:小止1995
来源:网络 阅读:391
  1. 创建一个线程:

 进程,是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念,竟争计算机系统资源的基本单位。每一个进程都有一个自己的地址空间,即进程空间或(虚空间)。进程空间的大小 只与处理机的位数有关,一个 16 位长处理机的进程空间大小为 216 ,而 32 位处理机的进程空间大小为 232 。进程至少有 5 种基本状态,它们是:初始态,执行态,等待状态,就绪状态,终止状态。

    线程,在网络或多用户环境下,一个服务器通常需要接收大量且不确定数量用户的并发请求,为每一个请求都创建一个进程显然是行不通的,——无论是从系统资源开销方面或是响应用户请求的效率方面来看。因此,操作系统中线程的概念便被引进了。线程,是进程的一部分,一个没有线程的进程可以被看作是单线程的。线程有时又被称为轻权进程或轻量级进程,也是 CPU 调度的一个基本单位。

关系:进程拥有一个完整的虚拟地址空间,不依赖于线程而独立存在;反之,线程是进程的一部分,没有自己的地址空间,与进程内的其他线程一起共享分配给该进程的所有资源

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

Compile and link with -pthread.

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 #include<stdlib.h>
  4 void* thread_run(void* id)
  5 {
  6     while(1)
  7     {
  8         printf("this is a thread\n");
  9         sleep(1);
 10     }
 11     return (void*)0;
 12 }
 13 
 14 int main()
 15 {
 16     pthread_t tid;
 17     int ret=pthread_create(&tid,NULL,thread_run,NULL);
 18     while(1)
 19     {
 20         printf("this is main thread\n");
 21         sleep(2);
 22     }
 23     return 0;
 24 }

运行结果:两个线程同时执行自己代码

线程的基本操作

2.线程等待:int pthread_join(pthread_t thread, void **retval);(以阻塞形式等待)

Compile and link with -pthread.

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 #include<stdlib.h>
  4 void* thread_run(void* id)
  5 {
  6     int count=5;
  7     while(count--)
  8     {
  9         printf("this is a thread\n");
 10         sleep(1);
 11     }
 12     return (void*)0;
 13 }
 14 
 15 int main()
 16 {
 17     pthread_t tid;
 18     int ret=pthread_create(&tid,NULL,thread_run,NULL);
 19     int count=10;
 20     while(count--)
 21     {
 22         printf("this is main thread\n");
 23         sleep(2);
 24     }
 25     void* retval=0;
 26     pthread_join(tid,&retval);
 27     printf("retval: %d\n",(int)retval);
 28     return 0;
 29 }

线程的基本操作

3.线程终止(4种)

(1)exit(1)直接终止进程

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 #include<stdlib.h>
  4 void* thread_run(void* id)
  5 {
  6     int count=5;
  7     while(count--)
  8     {
  9         printf("this is a thread\n");
 10         sleep(1);
 11     }
 12     exit(1);
 13 }
 14 
 15 int main()
 16 {
 17     pthread_t tid;
 18     int ret=pthread_create(&tid,NULL,thread_run,NULL);
 19     int count=10;
 20     while(count--)
 21     {
 22         printf("this is main thread\n");
 23         sleep(2);
 24     }
 25     void* retval=0;
 26     pthread_join(tid,&retval);
 27     printf("retval: %d\n",(int)retval);
 28     return 0;
 29 }

运行结果

线程的基本操作

(2)pthread_exit((void*)s);

#include <pthread.h>

void pthread_exit(void *retval);

Compile and link with -pthread.

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 #include<stdlib.h>
  4 void* thread_run(void* id)
  5 {
  6     int count=5;
  7     int ret=3;
  8     while(count--)
  9     {
 10         printf("this is a thread\n");
 11         sleep(1);
 12     }
 13     pthread_exit((void*)ret);
 14 }
 15 
 16 int main()
 17 {
 18     pthread_t tid;
 19     int ret=pthread_create(&tid,NULL,thread_run,NULL);
 20     int count=10;
 21     while(count--)
 22     {
 23         printf("this is main thread\n");
 24         sleep(2);
 25     }
 26     void* retval=0;
 27     pthread_join(tid,&retval);
 28     printf("retval: %d\n",(int)retval);
 29     return 0;
 30 }

线程的基本操作

(3)线程可被取消:

 #include <pthread.h>

 int pthread_cancel(pthread_t thread);

 Compile and link with -pthread.

a.被自己取消

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 #include<stdlib.h>
  4 void* thread_run(void* id)
  5 {
  6     int count=5;
  7     while(count--)
  8     {
  9         printf("this is a thread\n");
 10         sleep(1);
 11     }
 12     pthread_cancel(pthread_self());
 13 }
 14 
 15 int main()
 16 {
 17     pthread_t tid;
 18     int ret=pthread_create(&tid,NULL,thread_run,NULL);
 19     int count=10;
 20     while(count--)
 21     {
 22         printf("this is main thread\n");
 23         sleep(2);
 24     }
 25     void* retval=0;
 26     pthread_join(tid,&retval);
 27     printf("retval: %d\n",(int)retval);
 28     return 0;
 29 }

线程的基本操作

b.被主线程取消

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 #include<stdlib.h>
  4 void* thread_run(void* id)
  5 {
  6     int count=5;
  7     while(count--)
  8     {
  9         printf("this is a thread\n");
 10         sleep(1);
 11     }
 12 }
 13 
 14 int main()
 15 {
 16     pthread_t tid;
 17     int ret=pthread_create(&tid,NULL,thread_run,NULL);
 18     int count=3;
 19     while(count--)
 20     {
 21         printf("this is main thread\n");
 22         sleep(1);
 23     }
 24     pthread_cancel(tid);
 25     void* retval=0;
 26     pthread_join(tid,&retval);
 27     printf("retval: %d\n",(int)retval);
 28     return 0;
 29 }

线程的基本操作

 1 #include<stdio.h>
  2 #include<pthread.h>
  3 void* pthread_run1(void* arg)
  4 {
  5     int count=5;
  6     while(count--)
  7     {
  8         printf("this is a pthread,id: %d, pthread_id:%u\n",(int)arg,pthread_    self());
  9     }
 10     return (void*)1;
 11 }
 12 void* pthread_run2(void* arg)
 13 {
 14     int count=5;
 15     while(count--)
 16     {
 17         printf("this is a pthread,id: %d, pthread_id:%u\n",(int)arg,pthread_    self());
 18     }
 19     pthread_exit((void*)2);
 20 }
 21 void* pthread_run3(void* arg)
 22 {
 23     int count=5;
 24     while(count--)
 25     {
 26         printf("this is a pthread,id: %d, pthread_id:%u\n",(int)arg,pthread_    self());
 27     }
 28     pthread_cancel(pthread_self());
 29 }
 30 int main()
 31 {
 32     pthread_t tid1,tid2,tid3;
 33     pthread_create(&tid1,NULL,pthread_run1,(void*)1);
 34     pthread_create(&tid2,NULL,pthread_run2,(void*)2);
 35     pthread_create(&tid3,NULL,pthread_run3,(void*)3);
 36     void* ret=NULL;
 37     pthread_join(tid1,&ret);
 38     printf("pthread_id: %u,ret: %d\n",tid1,(int)ret);
 39     pthread_join(tid2,&ret);
 40     printf("pthread_id: %u,ret: %d\n",tid2,(int)ret);
 41     pthread_join(tid3,&ret);
 42     printf("pthread_id: %u,ret: %d\n",tid3,(int)ret);
 43     return 0;
 44 }

线程的基本操作

线程的分离:在任何一个时间点上, 线程是可结合的( joinable)或者是分离的( detached) 。一个可结合的线程能够被其他线程收回其资源和杀死。在被其他线程回收之前,它的存储器资源(例如栈)是不释放的。 相反, 一个分离的线程是不能被其他线程回收或杀死的,它的存储器资源在它终止时由系统自动动释放。

 1 #include<pthread.h>
  2 #include<stdio.h>
  3 void* pthread_run(void* arg)
  4 {
  5     int count=5;
  6     while(count--)
  7     {
  8         printf("this is thread\n");
  9         sleep(1);
 10     }
 11 }
 12 int main()
 13 {
 14     pthread_t tid;
 15     pthread_create(&tid,NULL,pthread_run,NULL);
 16     void* ret=NULL;
 17     pthread_detach(tid);
 18     sleep(1);
 19     pthread_join(tid,&ret);
 20     printf("ret: %d,errstring:%s\n",(int)ret,strerror((int)ret));
 21 }

在设置分离后,依旧等待退出,会返回错误码。

推荐阅读:
  1. MySQL的基本操作
  2. Oracle的基本操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

线程

上一篇:Python内存分配

下一篇:Python-变量、字符串

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》