北京清控人居建设集团网站,想用wordpress建立网站,北京seo培训机构,百度实景360度地图目录
1、线程
1.1线程Thread
1.2线程特点
1.3线程函数的原型
1.4Linux对于pthread的API的支持
1.4.1创建一个线程
1.4.2线程的退出
1.5资源分离
2、线程的同步/互斥机制
2.1线程互斥锁
2.1.1初始化线程互斥锁
2.2线程互斥锁的PV 操作
2.2.1P 操作#xff08;上锁…目录
1、线程
1.1线程Thread
1.2线程特点
1.3线程函数的原型
1.4Linux对于pthread的API的支持
1.4.1创建一个线程
1.4.2线程的退出
1.5资源分离
2、线程的同步/互斥机制
2.1线程互斥锁
2.1.1初始化线程互斥锁
2.2线程互斥锁的PV 操作
2.2.1P 操作上锁操作
2.2.2V 操作解锁操作
2.2.3线程互斥锁的销毁操作
3、生产者消费者模型
4、线程条件变量
4.1线程条件变量API
4.1.1初始化/销毁条件变量
4.1.2等待一个条件变量
4.1.3唤醒线程/触发条件变量 谢谢帅气美丽且优秀的你看完我的文章还要点赞、收藏加关注 没错说的就是你不用再怀疑 希望我的文章内容能对你有帮助一起努力吧 1、线程
前面讲到进程为了并发执行任务程序现代操作系统才引进 进程 的概念
分析
创建开销问题创建一个进程开销大 子进程需要拷贝父进程的整个地址空间通信开销问题进程间的通信 需要用第三方如内核P1 - copy - 内核 - copy - P2进程间通信代价或者开销也是很大的。进程的地址空间是独立要通信的话需要用第三方的空 间。
于是就有人提出能不能在 同一个同一个进程内部进程地址空间中进行任务的并发线程/轻量级进程
1.1线程Thread
线程是一个比进程更小的活动单位。它是进程中的执行路径执行分支线程也是并发的一种形式。 进程内部可以存在多个线程它并发执行但是进程内部的所有的线程共享整个进程的地址空间
main 函数进程的主线程 1.2线程特点
创建一个线程要比创建进程开销要小很多 因为创建一个线程的话不需要拷贝进程的地址空间实现线程间的通信会更加方便 因为进程内部所有线程共享整个进程地址空间线程也是一个动态概念 线程进程状态图 就绪态 ready运行态 running阻塞态 blocking有了线程的概念之后 系统的调度单位就从进程变为线程资源的分配还是以进程为单位
线程是进程内部的一个指令的执行分支多个线程就是多个指令序列的并发执行。这些指令必须在函数 内部线程的指令部分肯定是封装一个函数的内部的。这个函数就称之为线程函数一个线程在创 建之后要执行的指令全部封装在该函数内部这个线程函数执行完毕之后该线程的任务也就执行完 了。
1.3线程函数的原型 Thread 的实现有多种比较常见的为 POSIX 线程 pthread
1.4Linux对于pthread的API的支持
1.4.1创建一个线程
pthread_create 创建一个线程启动一个线程 #include iostream
#include pthread.h
#include unistd.hstatic int st_val 0;/*线程函数用于新线程创建之后调用的
*/
void *new_thread(void *data)
{for(;st_val 20;st_val){sleep(1);}st_val 0;pthread_exit(st_val);
}int main(int argc,const char *argv[])
{// 定义线程id的变量pthread_t tid;// 创建线程int ret pthread_create(tid,NULL,new_thread,NULL);if(ret -1){perror(线程创建失败:);return -1;}// 等待子线程把st_val累加到一百while(1){if(st_val 20)break;std::cout st_val: st_val std::endl;sleep(1);}return 0;
}
1.4.2线程的退出
线程函数的退出线程函数的返回在线程执行的任意时刻调用 pthread_exit被别人干掉 cancel 被别人取消其他线程调用 pthread_cancel t1:pthread_cancel(t2) t1 调用取消函数取消 t2 , t2 不一定会被取消 因为 t2 能不能被其他线程取消取决于 t2 线程的一个属性 取消属性 它是否可以被 cancelled #include iostream
#include pthread.h
#include unistd.hstatic int st_val 0;/*线程函数用于新线程创建之后调用的
*/
void *new_thread(void *data)
{int oldstate;pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,oldstate);for(;st_val 20;st_val){sleep(1);}st_val 0;
}int main(int argc,const char *argv[])
{// 定义线程id的变量pthread_t tid;// 创建线程int ret pthread_create(tid,NULL,new_thread,NULL);if(ret -1){perror(线程创建失败:);return -1;}// 等待子线程把st_val累加到一百while(1){if(st_val 20)break;std::cout st_val: st_val std::endl;if(st_val 5){pthread_cancel(tid);break;}sleep(1);}return 0;
}这个属性叫做 可以被取消属性 PTHREAD_CANCEL_ENABLE 表示该线程可以被取消PTHREAD_CANCEL_DISABLE 表示该线程不能被取消
一个线程退出了并不是所有资源都会释放。一个线程的退出它资源释放全部被释放取决于一个属 性
1.5资源分离
detach 分离属性 ENABLE 分离资源属性 该线程结束它的所有资源都会自动释放。DISABLE 不分离资源 该线程结束会有部分资源不会自动释放需要其他线程调用 pthread_join 这个函数 才能完全释放。默认是不分离属性。
#include iostream
#include pthread.h
#include unistd.hstatic int st_val 0;/*线程函数用于新线程创建之后调用的
*/
void *new_thread(void *data)
{// 进入线程函数第一时间就设置分离属性// pthread_detach(pthread_self()); // 设置了资源分离一般就不需要join了for(;st_val 20;st_val){sleep(1);}st_val 0;pthread_exit(st_val);
}int main(int argc,const char *argv[])
{// 定义线程id的变量pthread_t tid;// 创建线程int ret pthread_create(tid,NULL,new_thread,NULL);if(ret -1){perror(线程创建失败:);return -1;} std::cout 等待线程结束 std::endl;int *tid_ret_val NULL;pthread_join(tid,(void**)tid_ret_val);std::cout return value : *tid_ret_val std::endl;return 0;
}2、线程的同步/互斥机制
为了线程之间能够去有序的访问共享资源引用 信号量机制
信号量 System V 和 POSIX 信号量线程互斥锁
2.1线程互斥锁
线程互斥锁也是 信号量 只不过线程互斥锁存在于进程地址空间用于线程间同步和互斥操作线程 互斥锁它的效率相对信号量来说要高。
线程互斥锁使用 pthread_mutex_t 的类型来描述一个锁
安装线程 POSIX 帮助手册 sudo apt-get install manpages-posix-dev // 安装posix 帮助手册
2.1.1初始化线程互斥锁 2.2线程互斥锁的PV 操作
2.2.1P 操作上锁操作 2.2.2V 操作解锁操作 2.2.3线程互斥锁的销毁操作 #include iostream
#include pthread.h
#include unistd.h// 模拟内存
static int memory 100;// 互斥锁
pthread_mutex_t mutex;// 线程函数模拟病毒
void *thread_trylock(void *data)
{// 等待一秒防止线程一进来就直接上锁sleep(1);// 记录自己抢占了多内存int self_memory 0;// 循环条件while(memory 0){// 上锁if(0 pthread_mutex_trylock(mutex)){// 中间的代码就是临界区memory - 10;self_memory 10;sleep(1);// 解锁pthread_mutex_unlock(mutex);std::cout 嘿嘿咱“尝试上锁病毒” 抢占了10字节内存! std::endl;}elsestd::cout 哎咱“尝试上锁病毒”没有抢内存 std::endl;sleep(1);}std::cout 嘿嘿咱“尝试上锁病毒” 总共抢占了 self_memory 字节内存 std::endl;return nullptr;
}void *thread_lock(void *data)
{// 等待一秒防止线程一进来就直接上锁sleep(1);// 记录自己抢占了多内存int self_memory 0;// 循环条件while(memory 0){// 上锁pthread_mutex_lock(mutex);// 中间的代码就是临界区memory - 10;self_memory 10;sleep(1);// 解锁pthread_mutex_unlock(mutex);std::cout 嘿嘿咱“死锁上锁病毒” 抢占了10字节内存 std::endl;sleep(1);}std::cout 嘿嘿咱“死锁上锁病毒” 总共抢占了 self_memory 字节内存 std::endl;return nullptr;
}void *thread_timedlock(void *data)
{// 等待一秒防止线程一进来就直接上锁sleep(1);//等待的时间struct timespec tm;tm.tv_sec 1;tm.tv_nsec 0;// 记录自己抢占了多内存int self_memory 0;// 循环条件while(memory 0){// 上锁if(0 pthread_mutex_timedlock(mutex,tm)){// 中间的代码就是临界区memory - 10;self_memory 10;sleep(1);// 解锁pthread_mutex_unlock(mutex);std::cout 嘿嘿咱“等待上锁病毒” 抢占了10字节内存! std::endl;}else{std::cout 哎难受 咱“等待上锁病毒” 等了一秒都没有抢占到内存 std::endl;}sleep(1);}std::cout 嘿嘿咱“等待上锁病毒” 总共抢占了 self_memory 字节内存 std::endl;return nullptr;
}int main()
{pthread_mutex_init(mutex,NULL);pthread_t tid1;pthread_t tid2;pthread_t tid3;// 启动线程pthread_create(tid1,NULL,thread_lock,NULL);pthread_create(tid1,NULL,thread_trylock,NULL);pthread_create(tid1,NULL,thread_timedlock,NULL);// 阻塞等待子线程结束// pthread_join(tid1,NULL);// pthread_join(tid2,NULL);// pthread_join(tid3,NULL);while(1);pthread_mutex_destroy(mutex);return 0;
}
3、生产者消费者模型
生产者消费者模型利用厂商和消费者关系由生产者线程进行生产产生任务再由消费者线程消 费执行任务没有任务的时候消费者等待生产者产生任务。 共享资源的互斥访问问题 信号量/线程互斥锁当缓冲区生产者没有产出的时候没有数据的时候消费者应该怎么办 1. 不停的去测试看有没有数据。 轮询访问但是轮询有缺陷一直在访问浪费CPU资源。轮询有时间差占用总线 Is always busy让出CPU,当有数据的时候再唤醒我 wake up ,线程条件变量 同步
4、线程条件变量
线程条件变量在多线程程序设计中可以用 条件变量 为表示一个特定的条件或者是事件
pthread_cond_t 来描述一个条件变量类型
至于条件变量到底是一个什么事件或者说表示一个什么条件完全由程序猿去解释这个条件变量所代 表的含义
在条件变量上的三种操作
初始化等待一个条件变量等待该条件变量所表示的事件唤醒一个线程/触发条件变量唤醒了正在等待该事件的线程 4.1线程条件变量API
4.1.1初始化/销毁条件变量 4.1.2等待一个条件变量 4.1.3唤醒线程/触发条件变量 注意广播唤醒和单个唤醒的区别
广播唤醒唤醒所有等待的线程去执行任务但是任务可能不够分那么没分到的线程继续休眠单个唤醒随机唤醒一个线程执行任务其他线程继续休眠。
#include iostream
#include pthread.h
#include unistd.h
#include vector
using std::vector;// 线程互斥锁
pthread_mutex_t mutex;// 条件变量
pthread_cond_t cond;// 公共资源
FILE *fileNULL;
std::string str;// 消费者
void *NiuMa(void *data)
{while(1){pthread_mutex_lock(mutex);// 等待条件变量任务pthread_cond_wait(cond,mutex); // 这里卡死不是循环的卡死休眠让出了CPUif(str 退出){pthread_mutex_unlock(mutex); pthread_exit(NULL);}std::cout fwrite(str.c_str(),str.size(),1,file) std::endl;pthread_mutex_unlock(mutex);std::cout pthread_self() 线程成功写入内容 str std::endl;}}// 主线程生产者
int main()
{// 初始化pthread_mutex_init(mutex,NULL);pthread_cond_init(cond,NULL);// 打开文件file fopen(./1.txt,w);vectorpthread_t tids(10);// 创建10个线程for(int i 0;i 10;i){pthread_create(tids[0],NULL,NiuMa,NULL);}// 通过输入来发布任务while(1){std::cout 请输入内容: ;pthread_mutex_lock(mutex);std::cin str;pthread_mutex_unlock(mutex);if(str 退出){pthread_cond_broadcast(cond);break;}// 唤醒线程pthread_cond_signal(cond);sleep(1);}for(int i 0;i 10;i){pthread_join(tids[0],NULL);}pthread_mutex_destroy(mutex);pthread_cond_destroy(cond);fclose(file);return 0;
}
文章转载自: http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.fslrx.cn.gov.cn.fslrx.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.rfgc.cn.gov.cn.rfgc.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.fgxnb.cn.gov.cn.fgxnb.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.flqkp.cn.gov.cn.flqkp.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.cgtrz.cn.gov.cn.cgtrz.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.rnzwh.cn.gov.cn.rnzwh.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.xjkfb.cn.gov.cn.xjkfb.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.cpnlq.cn.gov.cn.cpnlq.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.rwmft.cn.gov.cn.rwmft.cn http://www.morning.junyaod.com.gov.cn.junyaod.com http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.pjtw.cn.gov.cn.pjtw.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.zxgzp.cn.gov.cn.zxgzp.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.jnoegg.com.gov.cn.jnoegg.com http://www.morning.lbjdx.cn.gov.cn.lbjdx.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn