当前位置: 首页 > news >正文

官方网站开发哪家好郑州一建劳务有限公司

官方网站开发哪家好,郑州一建劳务有限公司,吉林省网站制作公司有哪些,镇江网站建设哪家好目录 一、POSIX线程库 二、线程的创建 三、线程等待 四、线程终止 五、分离线程 六、线程ID#xff1a;pthread_t 1、获取线程ID 2、pthread_t 七、线程局部存储#xff1a;__thread 一、POSIX线程库 由于Linux下的线程并没有独立特有的结构#xff0c;所以Linux并…目录 一、POSIX线程库 二、线程的创建 三、线程等待 四、线程终止 五、分离线程 六、线程IDpthread_t 1、获取线程ID 2、pthread_t 七、线程局部存储__thread 一、POSIX线程库 由于Linux下的线程并没有独立特有的结构所以Linux并没有提供线程相关的接口。 而我们所说的pthread线程库是应用层的原生线程库。这个线程库并不是系统接口直接提供的而是由第三方帮我们提供的。 1、与线程有关的函数构成了一个完整的系列绝大多数函数的名字都是以“pthread_”打头的 2、要使用这些函数库要通过引入头文pthread.h 3、链接这些线程函数库时要使用编译器命令的“-lpthread”选项 二、线程的创建 pthread_create其功能就是创建线程。 NAMEpthread_create - create a new threadSYNOPSIS#include pthread.hint pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);Compile and link with -pthread. 参数说明 thread获取创建成功的线程ID该参数是一个输出型参数。 attr用于设置创建线程的属性传入nullptr表示使用默认属性。(我们一般不关心直接设为nullptr) start_routine该参数是一个函数指针表示线程启动后要执行的函数。 arg传给线程执行函数的参数。 返回值线程创建成功返回0失败返回错误码。返回值也可以自己设置返回给主线程。主线程通过pthread_join获取。 主线程当一个程序启动时就有一个进程被操作系统创建与此同时一个线程也立刻运行这个线程就叫做主线程。 下面我们让主线程调用pthread_create函数创建一个新线程 #include iostream #include unistd.h #include pthread.husing namespace std;void *thread_run(void *argc) {cout new thread pid: getpid() \n endl;sleep(20);return nullptr; }int main() {pthread_t tid;pthread_create(tid, nullptr, thread_run, (void *)thread 1);while (true){cout main thread pid: getpid() endl;sleep(1);}return 0; } 使用ps -aL命令可以显示当前的轻量级进程。 从上图我们看到两个线程的PID相同说明他们属于同一个进程。但是他们的LWP值不同说明他们是两个不同的线程。LWP就是轻量级进程的ID。 注在Linux中线程与内核的LWP是一一对应的实际上操作系统调度的时候是根据LWP调度的而不是PID只不过我们之前接触到的都是单线程进程其PID和LWP是相等的所以对于单线程进程来说调度时采用PID和LWP是一样的。 我们也可以让一个主线程创建多个新线程 #include iostream #include unistd.h #include string #include pthread.husing namespace std;void *thread_run(void *argc) {string name (char *)argc;while (true){cout name --- pid: getpid() \n endl;sleep(1);} }int main() {pthread_t tid[5];char name[64];for (int i 0; i 5; i){snprintf(name, sizeof(name), %s-%d, thread, i);pthread_create(tid i, nullptr, thread_run, (void *)name);sleep(1);}while (true){cout main thread pid: getpid() endl;sleep(3);}return 0; } 因为主线程和五个新线程都属于同一个进程所以它们的PID都是一样的。  三、线程等待 一个线程被创建出来那么这个线程就如同进程一般也是需要被等待的。如果主线程不对新线程进行等待那么这个新线程的资源也是不会被回收的。如果不等待会产生类似于“僵尸进程”的问题也就会造成内存泄漏。所以线程需要被等待。 pthread_join其功能就是进行线程等待 NAMEpthread_join - join with a terminated threadSYNOPSIS#include pthread.hint pthread_join(pthread_t thread, void **retval);Compile and link with -pthread. 参数说明 thread被等待线程的ID。 retval线程退出时的退出码信息。 返回值线程等待成功返回0失败返回错误码。 #include iostream #include unistd.h #include string #include pthread.husing namespace std;void *thread_run(void *argc) {int count 10;while (true){sleep(1);if (count 10)break;}cout new thread done ... quit endl;return nullptr; }int main() {pthread_t tid;pthread_create(tid, nullptr, thread_run, (void *)thread1);pthread_join(tid, nullptr);cout main thread wait done ... quit endl;return 0; } 第二个参数是用来获取新线程返回值的。主线程可以通过新线程的返回值拿到新线程的计算结果该结果也可以保存在堆空间上 include iostream #include unistd.h #include string #include pthread.husing namespace std;void *thread_run(void *argc) {int count 10;while (true){sleep(1);if (count 10)break;}cout new thread done ... quit endl;return (void *)10; }int main() {pthread_t tid;pthread_create(tid, nullptr, thread_run, (void *)thread1);void *ret nullptr;pthread_join(tid, ret);cout main thread wait done ... quit (long long)ret endl;return 0; } 四、线程终止 return最简单的终止线程的方式就是使用return返回一个返回值来终止线程。 pthread_exit其功能就是终止一个线程。终止线程不能使用exit因为它是用来终止进程的 参数retval设置退出结果。 NAMEpthread_exit - terminate calling threadSYNOPSIS#include pthread.hvoid pthread_exit(void *retval);Compile and link with -pthread. #include iostream #include unistd.h #include string #include pthread.husing namespace std;void *thread_run(void *argc) {int count 10;while (true){sleep(1);if (count 10)break;}cout new thread done ... quit endl;pthread_exit((void*)17); }int main() {pthread_t tid;pthread_create(tid, nullptr, thread_run, (void *)thread1);void *ret nullptr;pthread_join(tid, ret);cout main thread wait done ... quit (long long)ret endl;return 0; } pthread_cancel其功能是取消一个线程。 参数thread线程ID。 NAMEpthread_cancel - send a cancellation request to a threadSYNOPSIS#include pthread.hint pthread_cancel(pthread_t thread);Compile and link with -pthread. #include iostream #include unistd.h #include string #include pthread.husing namespace std;void *thread_run(void *argc) {string name (char *)argc;int count 10;while (true){sleep(1);if (count 10)break;}cout new thread done ... quit endl; }int main() {pthread_t tid;pthread_create(tid, nullptr, thread_run, (void *)thread1);void *ret nullptr;pthread_cancel(tid);pthread_join(tid, ret);cout main thread wait done ... quit (long long)ret endl;return 0; } 线程被取消线程等待时获取的退出码为-1。  五、分离线程 新线程退出后主线程需要对其进行pthread_join操作否则无法释放资源从而造成内存泄漏。 但如果主线程不关心新线程的返回值此时我们可以将该新线程进行分离后续当新线程退出时就会自动释放线程资源。 一个线程如果被分离了这个线程依旧要使用该进程的资源依旧在该进程内运行甚至这个线程崩溃了一定会影响其他线程只不过这个线程退出时不再需要主线程去join了当这个线程退出时系统会自动回收该线程所对应的资源。 pthread_detach其功能就是进行分离线程。一般是线程自己分离。 int pthread_detach(pthread_t thread); 参数说明thread被分离线程的ID。 返回值说明 线程分离成功返回0失败返回错误码。 #include iostream #include unistd.h #include string #include pthread.husing namespace std;void *thread_run(void *argc) {pthread_detach(pthread_self());int count 10;while (true){sleep(1);if (count 10)break;}cout new thread done ... quit endl;pthread_exit((void*)17); }int main() {pthread_t tid;pthread_create(tid, nullptr, thread_run, (void *)thread1);void *ret nullptr;cout main thread wait done ... quit (long long)ret endl;return 0; } 如果我们在线程分离了之后任然等待会怎么样呢 #include iostream #include unistd.h #include cerrno #include cstring #include pthread.husing namespace std;void *thread_run(void *argc) {pthread_detach(pthread_self());int count 9;while (true){sleep(1);if (count 10)break;}cout new thread done ... quit endl;pthread_exit((void *)17); }int main() {pthread_t tid;pthread_create(tid, nullptr, thread_run, (void *)thread1);sleep(2);int n pthread_join(tid, nullptr);cout n: n errstring: strerror(n) endl;return 0; } 六、线程IDpthread_t pthread_create函数会产生一个线程ID存放在第一个参数指向的地址中该线程ID和内核中的LWP是完全不一样的。内核中的LWP属于进程调度的范畴需要一个数值来唯一表示该线程。 那么pthread_t到底是什么类型呢 1、获取线程ID pthread_self获取线程的ID。 #include iostream #include unistd.h #include cerrno #include cstring #include pthread.husing namespace std;void *thread_run(void *argc) {int count 9;while (true){sleep(1);if (count 10)break;}cout new thread done ... quit new thread ID: pthread_self() endl;pthread_exit((void *)17); }int main() {pthread_t tid;pthread_create(tid, nullptr, thread_run, (void *)thread1);void *ret nullptr;sleep(2);cout main thread ID: pthread_self() endl;pthread_join(tid, ret);return 0; } 为什么线程的ID数值这么大呢下面我们就来讲一讲。  2、pthread_t 进程运行时线程动态库被加载到内存然后通过页表映射到进程地址空间中的共享区此时该进程内的所有线程都是能看到这个动态库的。 其中主线程采用的栈是进程地址空间中原生的栈而其余线程采用的栈就是由线程库帮我们在共享区中开辟的。 线程库给每个新线程提供属于自己的struct pthread当中包含了对应线程的各种属性每个线程还有自己的线程局部存储当中包含了对应线程被切换时的上下文数据。其中还有线程栈。如下图 所以线程ID本质就是进程地址空间共享区上对应的struct pthread的虚拟地址。  七、线程局部存储__thread 假设有一个全局变量g_val。我们知道各个线程是共享全局变量的。不同的线程可以对同一个全局变量进行操作。那么如果我们想让每个线程都拥有属于自己的g_val那么我们可以加上关键字__thread。这种现象就叫做线程局部存储。
http://www.hkea.cn/news/14583181/

相关文章:

  • 做网站销售电销好做吗wordpress加载js
  • 网站建设的基础内容虎牙网页游戏大厅
  • 电子商务网站建设实验报告网站需要怎么做的
  • 怎样做投资网站潜江资讯网 手机版
  • 苏州做网站便宜的公司触屏版网站源码
  • 安新建设局网站鞍山自适应网站制作
  • 淘宝上网站开发网站开发遇到的难题解决
  • 三只松鼠的网站建设的意义深圳移动网站建设公
  • 建设网站的市场环境百度企业服务平台
  • 织梦做的网站很老建站方案书备案
  • 宣城做网站公司电子商务网站的开发语言
  • 企业管理顾问东莞网站建设wordpress+视频边栏
  • 推广网站发布文章产品页面设计模板
  • 国内优秀网站欣赏厦门建设厅网站
  • 龙泉市旅游门户网站建设电子设计大赛网站开发
  • 网站建设 英文怎么说科技网站有哪些
  • 常州网站价格深圳网站建设深圳网
  • 桐城市住房与建设网站大二网络营销实训报告
  • 一个网站多个域名的seo优化浙江省建设业技术创新协会网站
  • idea网站开发教程wordpress插件转php
  • 电子商务网站建设作业淄博网站文章优化
  • 做网站免费搭建企业站群系统
  • 绍兴网站建设解决方案金融网站素材
  • php网站作业模版亳州市网站建设
  • 东莞南城网站开发公司电话迅睿cms建站教程
  • 南宁做网站价格优秀网站架构
  • 如何把网站做成软件网站域名解绑
  • 网站风格确定七牛云公司怎么样
  • 网页app生成器原理7个湖北seo网站推广策略
  • 网站 图片 自动往右移网络营销企业有哪些公司