1 What are Threads?

  1. 一个进程可以有多个线程
  2. 线程共享进程的资源,多进程是复制内存空间,彼此独立
  3. 线程被OS调度,意味着一个进程会使用超过100%的cpu(多核系统)

2 Hello POSIX Threads?

  1. POSIX became the standard interface for many system(C,Java,Python)

2.1 Creating a Thread

/* hello_pthread.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#include <pthread.h>void * hello_fun(void * args){printf("Hello World!\n");return NULL;
}int main(int argc, char * argv[]){pthread_t thread;  //thread identifier//create a new thread have it run the function hello_funpthread_create(&thread, NULL, hello_fun, NULL);//wait until the thread completespthread_join(thread, NULL);return 0;
}
  1. pthread_create()创建线程,运行hello_fun,并用thread指针接收
  2. pthread_join()主线程阻塞,直到thread执行结束
  3. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

2.2 Passing Arguments to a Thread

/* hello_args_pthread.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#include <pthread.h>void * hello_arg(void * args){char * str = (char *) args;printf("%s", str);return NULL;
}int main(int argc, char * argv[]){char hello[] = "Hello World!\n";pthread_t thread;  //thread identifier//create a new thread that runs hello_arg with argument hellopthread_create(&thread, NULL, hello_arg, hello);//wait until the thread completespthread_join(thread, NULL);return 0;
}

2.3 Joining Threads

  1. thread的join很像process的wait
  2. 线程之间可以相互join
    3.当主线程退出时,其他所有线程自动销毁,所以不会产生僵尸和浪费资源
/* hello_pthread_bad.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#include <pthread.h>void * hello_fun(){printf("Hello World!\n");return NULL;
}int main(int argc, char * argv[]){pthread_t thread;pthread_create(&thread, NULL, hello_fun, NULL);return 0;
}

2.4 Return values from threads

1.int pthread_join(pthread_t thread, void **retval);
2.类似于进程中的exit函数传递进程退出的状态,线程可以返回任何参数

2.5 Compiling POSIX threads

3 Threads and the OS

  1. 系统层面,pid和tid是相同的,既主线程和进程的id是相同的
  2. 线程内部tid和pid是不同的
/* hello_id_pthread.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>#include <pthread.h>//have to call syscall directly, no libc wrapper
pid_t gettid(){return (pid_t) syscall (SYS_gettid);
}void * hello_fun(void * args){//retrieve the thread_idpthread_t thread = pthread_self();//print all identifying informationprintf("THREAD: TID:%d PID:%d PthreadID:%lu\n", gettid(), getpid(), thread);return NULL;
}int main(int argc, char * argv[]){pthread_t thread;  //thread identifier//create a new thread have it run the function hello_funpthread_create(&thread, NULL, hello_fun, NULL);//print all identifying informationprintf("MAIN: TID:%d PID:%d \n", gettid(), getpid());//wait until the thread completespthread_join(thread, NULL);return 0;
}

3.2 Threads Running Like Processes

  1. C中每个线程可以占用一个cpu核

参考:
https://www.usna.edu/Users/cs/aviv/classes/ic221/s16/lec/28/lec.html#coderef-bad_ref

28 POSIX Threads相关推荐

  1. [并发并行]_[线程池]_[Programming With POSIX Threads的线程池实现分析1]

    场景 1.C++标准库没有提供线程池操作, 连Win32都没有集成线程池, 相比之下macOS完善多了, 至少有operations. 多线程在执行多任务时有很大优势, 比如同时管理多个设备, 多个s ...

  2. Pthread:POSIX Threads Programming

    POSIX Threads Programming Blaise Barney, Lawrence Livermore National Laboratory 译文点此 Table of Conten ...

  3. posix threads php,3分钟短文 | PHP多线程没用过,你可能错过了计算机最好的时代!...

    原标题:3分钟短文 | PHP多线程没用过,你可能错过了计算机最好的时代! 引言 别的语言都在一窝蜂地搞编译,搞虚拟机,搞多线程,提高效率.我们整天敲 PHP 代码,除了 Nginx 调用 php-f ...

  4. linux posix 线程池_linux多线程--POSIX Threads Programming

    linux多线程自己从接触很久也有不少实践,但总是觉得理解不够深刻,不够系统.借这篇文章试着再次系统学习一下linux多线程编程,理解编程的concept,细致看一下POSIX pthread API ...

  5. NPTL简介 (NATIVE POSIX Thread Library)

    POSIX Thread Library (NPTL)使Linux内核可以非常有效的运行使用POSIX线程标准写的程序.这里有一个测试数据,在32位机下,NPTL成功启动100000个线程只用了2秒, ...

  6. POSIX线程的同步

    当线程运行在同样的线程空间,线程们共享同样的内存和资源.这使它变得很容易对于线程来通信和共享数据,尽管它会发生两种问题:线程阻塞和内存不一致由于线程的同步对共享资源的修改.在这些情况下,线程同步变得很 ...

  7. VxWorks 6.9 内核编程指导之读书笔记 -- POSIX

    POSIX能力 VxWorks扩展了POSIX,为了移植,VxWorks提供了额外的POSIX接口作为可选组件.VxWorks实现了POSIX 1003.1(POSIX .1)一些传统接口以及POSI ...

  8. ANSI C标准库和POSIX库

    ANSI C POSIX库(标准C库除外) ANSI C headers assert.h stddef.h stdbool.h stdint.h stdarg.h stdio.h stdlib.h ...

  9. 优秀开源项目之三:高性能、高并发、高扩展性和可读性的网络服务器架构State Threads...

    译文在后面. State Threads for Internet Applications Introduction State Threads is an application library ...

最新文章

  1. 华为计算平台MDC810发布量产
  2. fragment类onresume里面刷新操作处理
  3. c#连接oracle11,C#连接Oracle 11g 无需安装Oracle客户端
  4. python dlib opencv人脸识别准确度_Python用opencv+dlib实现人脸识别
  5. 2018.6.8-岁岁年年人不同
  6. docke容器无法访问宿主主机的端口
  7. 人工智能,不止于技术的革命--WOT2017全球创新技术峰会开幕
  8. python垃圾回收价格表_深度解析Python垃圾回收机制(超级详细)
  9. Unity URP高度雾效果Shader
  10. mac上SVN简单几个命令
  11. 云计算作业001-电脑配置
  12. 【动态规划】区间dp:P1005矩阵取数
  13. deal.II链接PETSc过程记录
  14. M8TSC预览版0.5.1发布
  15. 电流纹波率取r = 0.4的原因 // 《精通开关电源设计》P48:r的最优值
  16. EovaJFinal在阿里云K8S实现多环境自动化部署
  17. 新年喜报!10人通过RHCA、60人通过RHCE!
  18. 捋一捋Kafka中的消费者API
  19. SpringBoot 中连接阿里云rds数据库
  20. Windows下vc开发chrome浏览器工程的一点粗浅理解和封装

热门文章

  1. stm32 python界面开发_【STM32开发】使用Qt Creator 开发STM32
  2. python逻辑取反运算符_python学习笔记------逻辑运算符
  3. Python绘制雷达图展示学生各科考试成绩
  4. 侏儒排序算法原理与Python实现
  5. java ssh框架流程图_SSH框架整合详细分析【执行流程】
  6. sql decimal 转string_音频怎么转文字?这个软件带你体验飞一般的感觉
  7. 基于递归的前序二叉树遍历实现
  8. 中心对称又是轴对称的图形_2020广东省考行测备考:图形推理之对称知多少
  9. extjs 关闭弹出窗口_【实用工具】一键关闭/开启Windows防火墙、禁止更新系统
  10. 5 查询一个小时前_2021国考成绩查询系统登录入口