step4 . day6 线程与线程的创建
1.什么是线程:线程是轻量级的进程,占用资源少,同一个进程的线程共享相同的地址空间,线程间切换时间少
2.线程概念分类:同步线程,互斥线程(防止同时修改临界资源),线程的PV操作等
3.线程相关命令 ps -eLf 查看所有线程,线程编译要链接库 -lpthread
4.线程相关函数
1)线程创建:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
tid(出错不能用perror打印) 线程属性null 线程回调函数 给线程函数传参(可以传值,强转)
线程注意事项:①线程里面不可以写exit,否则整个进程都退出,②main函数退出,整个程序就退出,线程不存在,③线程执行完毕线程关闭,资源不回收
2)线程回收:
pthread_join函数 对应进程的 waitpid
pthread_exit 线程退出函数 对应进程的 exit,返回的值可以被pthread_join函数接受
3)线程取消: pthead_cancel 一个线程可以取消另外一个线程(不保证结果)。添加pthread_testcancel()函数保证线程可以被正常取消
4)线程的detach:int pthread_detach(pthread_t thread),设置线程为detach属性,这样线程退出资源自动释放,无需使用pthread_join(pthread_self(),自己线程tid)
5.线程相关demo代码
1)两个线程干不同是事情
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#define N 32
void *timewrite(void *arg);
void *scanfwrite(void *arg);
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;
int re1,re2;
re1 = pthread_create(&tid1,NULL,timewrite,NULL);
if(re1){
printf("pthread_create 1:%s\n",strerror(re1));
return -1;
}
re2 = pthread_create(&tid2,NULL,scanfwrite,NULL);
pthread_detach(tid1);
if(re2){
printf("pthread_create 1:%s\n",strerror(re2));
return -1;
}
pthread_detach(tid1);
sleep(60);
pthread_cancel(tid1);
pthread_cancel(tid2);
return 0;
}
void *timewrite(void *arg){
FILE *fp;
fp = fopen("timelog.txt", "w");
if(fp == NULL){
perror("fopen timelog");
return NULL;
}
while(1){
time_t t = time(&t);
char buf[N] ={0};
strcpy(buf,ctime(&t));
fwrite(buf,1,strlen(buf),fp);
sleep(1);
pthread_testcancel();
}
}
void *scanfwrite(void *arg){
int fd;
char buf[N]={0};
fd = open("scanflog.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666);
if(fd < 0){
perror("open scanflog");
return NULL;
}
while(1){
fgets(buf, N, stdin);
write(fd,buf,strlen(buf));
pthread_testcancel();
}
2)线程锁,防止线程同时修改临界资源区(死锁是线程相互等待各自资源,避免死锁方案①一次申请②使用一个锁)
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
pthread_mutex_t lock;
void * writefunc(void *arg){
int fd1;
char buf[32];
strcpy(buf,(char*)arg);
pthread_mutex_lock(&lock);
fd1 = open("log.txt",O_APPEND|O_WRONLY);
printf("buf = %s\n",buf);
write(fd1,buf,strlen(buf));
close(fd1);
pthread_mutex_unlock(&lock);
}
int main(int argc, const char *argv[])
{
int re1,re2;
pthread_t tid1,tid2;
char *buf1 = "hello world\n";
char *buf2 = "how are you\n";
int fd;
pthread_mutex_init(&lock,NULL);
fd = open("log.txt", O_CREAT|O_TRUNC,0777);
if(fd<0){
perror("open");
return -1;
}
close(fd);
re1 = pthread_create(&tid1, NULL,writefunc,(void*)buf1);
if(re1){
printf("pthread_create 1:%s\n",strerror(re1));
return -1;
}
pthread_detach(tid1);
re2 = pthread_create(&tid1, NULL,writefunc,(void*)buf2);
if(re2){
printf("pthread_create 1:%s\n",strerror(re2));
return -1;
}
pthread_detach(tid2);
sleep(10);
return 0;
}
3)线程的PV操作(生产者消费之概念)
sem_t sem;
void * writefunc1(void *arg){
int fd1;
char buf[32];
strcpy(buf,(char*)arg);
fd1 = open("log.txt",O_APPEND|O_WRONLY);
printf("buf = %s\n",buf);
write(fd1,buf,strlen(buf));
close(fd1);
// sem_post(&sem); //释放生产资源
}
void * writefunc2(void *arg){
// sem_wait(&sem); //申请消费资源
int fd1;
char buf[32];
strcpy(buf,(char*)arg);
fd1 = open("log.txt",O_APPEND|O_WRONLY);
printf("buf = %s\n",buf);
write(fd1,buf,strlen(buf));
close(fd1);
}
int main(int argc, const char *argv[])
{
int re1,re2;
pthread_t tid1,tid2;
char *buf1 = "hello world\n";
char *buf2 = "how are you\n";
int fd;
// sem_init(&sem,0,0); //初始化生产队列
fd = open("log.txt", O_CREAT|O_TRUNC,0777);
if(fd<0){
perror("open");
return -1;
}
close(fd);
re1 = pthread_create(&tid1, NULL,writefunc1,(void*)buf1);
if(re1){
printf("pthread_create 1:%s\n",strerror(re1));
return -1;
}
pthread_detach(tid1);
re2 = pthread_create(&tid1, NULL,writefunc2,(void*)buf2);
if(re2){
printf("pthread_create 1:%s\n",strerror(re2));
return -1;
}
pthread_detach(tid2);
sleep(10);
return 0;
}
转载于:https://www.cnblogs.com/huiji12321/p/11319002.html
step4 . day6 线程与线程的创建相关推荐
- 什么是Python线程?Python线程如何创建?
相信正在学习Python技术或者对Python语言有一定了解的人对于Python线程应该都不陌生,但是也有刚接触Python的小伙伴对于Python线程并不了解,今天小编就跟大家聊聊什么是Python ...
- 【Linux 内核】进程管理 ( 内核线程概念 | 内核线程、普通进程、用户线程 | 内核线程与普通进程区别 | 内核线程主要用途 | 内核线程创建函数 kernel_thread 源码 )
文章目录 一.内核线程概念 二.内核线程.普通进程.用户线程 三.内核线程.普通进程区别 四.内核线程主要用途 五.内核线程创建函数 kernel_thread 源码 一.内核线程概念 直接 由 Li ...
- 【C++ 语言】线程 ( 线程创建方法 | 线程标识符 | 线程属性 | 线程属性初始化 | 线程属性销毁 | 分离线程 | 线程调度策略 | 线程优先级 | 线程等待 )
文章目录 I 线程创建方法 II 线程执行函数 III 线程标识符 IV 线程属性 V 线程属性 1 ( 分离线程 | 非分离线程 ) VI 线程属性 2 ( 线程调度策略 ) VII 线程属性 3 ...
- Java Review - 创建线程和线程池时建议指定与业务相关的名称
文章目录 概述 线程 不指定线程名称为何难定位问题 Thread默认的线程名称 指定线程名称 线程池 不指定线程池名称为何难定位问题 指定线程名称 自定义线程名称 小结 概述 在日常开发中,当在一个应 ...
- Java线程详解(2)-创建与启动
一.定义线程 1.扩展java.lang.Thread类. 此类中有个run()方法,应该注意其用法:public void run() 如果该线程是使用独立的Runnable运行对象构造的,则调用该 ...
- 并发编程——线程——线程的理论和创建以及守护线程
一.什么是线程 线程是操作系统能够进行运算调度的最小单位. 它被包含在进程之中,是进程中的实际运作单位. 一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任 ...
- 线程池原理及创建(C++实现)
本文给出了一个通用的线程池框架,该框架将与线程执行相关的任务进行了高层次的抽象,使之与具体的执行任务无关.另外该线程池具有动态伸缩性,它能根据执行任务的轻重自动调整线程池中线程的数量.文章的最后,我们 ...
- C++线程池原理及创建(转)
C++线程池原理及创建(转) 来自http://www.cnblogs.com/cpper-kaixuan/p/3640485.html 本文给出了一个通用的线程池框架,该框架将与线程执行相关的任务进 ...
- pthread 立即停止线程_pthread线程的终止退出 | 线程的大量创建
1. 线程只是从启动例程中返回,返回值是线程的退出码: 2. 线程调用了pthread_exit函数: 3. 线程可以被同一进程中的其他线程取消. ************************** ...
最新文章
- java判断是否为数组_JS如何判断是否是数组?
- 移动端、PC端网站优化需齐头并进
- Excel VBA开发中数字签名的管理
- pytorch笔记——autograd和Variable
- SAP 外币金额 汇率 转换
- numpy(5)-astype
- boost::geometry::intersection用法的测试程序
- 前端如何实现音乐盒胶盘的转动_郑州Web前端入门教程之如何实现图片优化?
- echarts地图 编辑颜色
- 使用@Order调整配置类加载顺序
- 【OpenCV】特征检测器 FeatureDetector
- [Matlab] subs 函数 将符号变量代入符号表达式不需要加引号
- 华为高薪吸纳“天才” !任正非:进来华为,就没有“天才少年”这个名词了
- 深度挖坑系列之三:人脸识别,角度空间,欧氏空间,margin
- Dango搭建个人博客:前言
- AI:人工智能领域算法思维导图集合之有监督学习/无监督学习/强化学习类型的具体算法简介(预测函数/优化目标/求解算法)、分类/回归/聚类/降维算法模型选择思路、11类机器学习算法详细分类之详细攻略
- python脚本一键抓考试资料网答案
- 【Linux】ps和top进程管理命令
- 信通院牵头数列科技参与主编的《信息系统稳定性保障能力建设指南》正式发布
- Linux之基础命令
热门文章
- xy坐标转换为极坐标_CAD制图软件新手攻略:坐标系及标注坐标
- python一个try块后接一个或多个finally块_Python *与*语句完全等同于一个try – (除了) – finally块?...
- Dll入口函数参数详解
- 简述enumerate函数
- Win7环境下IPython Notebook的安装
- px4原生源码学习二--实时操作系统篇
- HDU Problem - 6396 Swordsman(优先队列,模拟)
- java6_64.tar配置_centos6.5_64 java 环境变量配置
- Redis设计与实现RDB持久化
- C++ int转string的几种方法比较