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

淄博网站建设设计wordpress 发布工具

淄博网站建设设计,wordpress 发布工具,wordpress怎么釆集文章,百度竞价登录c提供了互斥量#xff1a;mutex和条件变量#xff1a;condition_variable#xff0c;但是并没有信号量#xff1a;semaphore。而linux和windows系统库会提供的。下面简单介绍一下信号量的特性#xff0c;然后给出一个简单的demo#xff0c;使用mutex condition_variable…c提供了互斥量mutex和条件变量condition_variable但是并没有信号量semaphore。而linux和windows系统库会提供的。下面简单介绍一下信号量的特性然后给出一个简单的demo使用mutex condition_variable 来实现信号量。 信号量的定义 信号量是一个整数count提供两个原子操作P操作和V操作 P操作(wait操作)count减1如果count 0那么挂起执行线程 V操作(signal操作)count加1如果count 0那么唤醒一个执行线程 mutex互斥量相当于一把锁lock的状态为0 1也就是lock状态与unlock状态。如果现在有多把锁数量count最初被设定为n取1把锁count–如果发现锁的数量小于0也就是没有锁了此时就需要wait也可以说是suspend or block直到别人释放出一把锁。取完锁要还一把锁也就是count如果发现此时count0说明此时有人在等待锁就唤醒一个等待的线程把锁给他。 当count 1时就相当于mutex了。 如何实现信号量 1、首先需要一个int or long变量作为count 2、然后由于PV操作是原子操作所以至少需要一个mutex来保证互斥 3、需要挂起线程又需要唤醒线程所以也需要用到condition_variable 代码 #include iostream #include mutex #include condition_variable #include thread namespace Solution1 {class semaphore {private:int count;int wakeups; // 辅助变量要唤醒的线程数初值为0std::mutex mutex;std::condition_variable cond;public:semaphore(int value 1) : count(value), wakeups(0) {}void wait() // P操作{std::unique_lockstd::mutex lock(mutex);if (--count 0) {cond.wait(lock, []()-bool{return wakeups 0;});--wakeups;}}void signal() // V操作{std::lock_guardstd::mutex lock(mutex);if (count 0) {wakeups;cond.notify_one();}}};std::mutex printMutex;Solution1::semaphore ba(0) , cb(0), dc(0);void a(){ba.wait(); // b-astd::lock_guardstd::mutex lock(printMutex);std::cout thread a std::endl;}void b(){cb.wait(); // c-bstd::lock_guardstd::mutex lock(printMutex);std::cout thread b std::endl;ba.signal(); // b-a}void c(){dc.wait(); // d-cstd::lock_guardstd::mutex lock(printMutex);std::cout thread c std::endl;cb.signal(); // c-b}void d(){std::lock_guardstd::mutex lock(printMutex);std::cout thread d std::endl;dc.signal(); // d-c} };namespace Solution2 {class semaphore {private:int count;std::mutex mutex;std::condition_variable cond;public:semaphore(int value 1) : count(value){}void wait() // P操作{std::unique_lockstd::mutex lock(mutex);if (--count 0) {cond.wait(lock);}}void signal() // V操作{std::lock_guardstd::mutex lock(mutex);if (count 0) {cond.notify_one();}}};std::mutex printMutex;Solution1::semaphore ba(0) , cb(0), dc(0);void a(){ba.wait(); // b-astd::lock_guardstd::mutex lock(printMutex);std::cout thread a std::endl;}void b(){cb.wait(); // c-bstd::lock_guardstd::mutex lock(printMutex);std::cout thread b std::endl;ba.signal(); // b-a}void c(){dc.wait(); // d-cstd::lock_guardstd::mutex lock(printMutex);std::cout thread c std::endl;cb.signal(); // c-b}void d(){std::lock_guardstd::mutex lock(printMutex);std::cout thread d std::endl;dc.signal(); // d-c} };// 测量一个函数的运行时间 template class T void measure(T func) {using namespace std::chrono;auto start system_clock::now();// funcfunc();durationdouble diff system_clock::now() - start;std::cout 执行了 diff.count() 秒 std::endl; }int main() {measure([](){std::thread th1(Solution1::a), th2(Solution1::b), th3(Solution1::c), th4(Solution1::d);th1.join();th2.join();th3.join();th4.join();std::cout ending std::endl;});measure([](){std::thread th1(Solution2::a), th2(Solution2::b), th3(Solution2::c), th4(Solution2::d);th1.join();th2.join();th3.join();th4.join();std::cout ending std::endl;});return 0; }在linux系统下运行效果如下 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ touch main.cpp dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ vim main.cpp dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ g -pthread -o main main.cpp dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000541065秒 thread d thread c thread b thread a ending 执行了0.000292463秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000422226秒 thread d thread c thread b thread a ending 执行了0.000189556秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000549342秒 thread d thread c thread b thread a ending 执行了0.000312412秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000521796秒 thread d thread c thread b thread a ending 执行了0.00043399秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000426875秒 thread d thread c thread b thread a ending 执行了0.000244544秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000445409秒 thread d thread c thread b thread a ending 执行了0.000345057秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000377516秒 thread d thread c thread b thread a ending 执行了0.000258996秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000523052秒 thread d thread c thread b thread a ending 执行了0.00027911秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000517352秒 thread d thread c thread b thread a ending 执行了0.000395749秒 dyydyy-Lenovo-ThinkBook-14-IIL:~/Desktop/HeartBeat$ ./main thread d thread c thread b thread a ending 执行了0.000488651秒 thread d thread c thread b thread a ending 执行了0.000392515秒参考 c11中信号量(semaphore)的实现 | 陆仁贾 深层次探讨mutex与semaphore之间的区别 下面的这个代码比较复杂 线程同步之信号量代码实现方法2条件变量mutex互斥量
http://www.zqtcl.cn/news/315542/

相关文章:

  • 云南专业网站建设上海百度移动关键词排名优化
  • 如何搭建一个完整的网站wordpress 小程序开发
  • 外贸网站建设关键点为网站网站做代理被判缓刑
  • 网站免费正能量小说台州百度关键词优化
  • 保定自助建站做静态网站
  • 旅游网站对比模板免费招收手游代理
  • phpstudy网站建设教程wordpress破解管理员帐号
  • 商务网站规划与建设心得北京小程序制作首选华网天下
  • 果洛电子商务网站建设多少钱公司网站建设选什么服务器
  • 莱芜做网站公司网站建设表单教案
  • 建设酒类产品网站的好处遵义网站制作费用
  • 高端网站设计价格wordpress登录下载附件
  • 国内有名的网站设计公司wordpress缓存插件比拼
  • 网站的建设和推广直播营销策划方案范文
  • 做购物平台网站 民治百度导航地图下载
  • 东莞市主营网站建设服务机构青岛建站公司电话
  • 做网站技术wordpress漂亮手机网站模板下载
  • 网站怎么更新网页内容网络推广怎么找客户
  • 如何编写网站建设销售的心得适合装饰公司的名字
  • 有什么免费建网站网站pr查询
  • flash+xml网站模板简述网站制作的一般流程
  • 成都私人做网站建设怎么切页面做网站
  • 聊城做网站的公司论坛外链代发
  • 廊坊企业自助建站网站框架设计好后怎么做
  • 手机网站建设效果wordpress 目录改变
  • 做商城网站的项目背景图片c2750服务器做网站行吗
  • 北京市专业网站建设wordpress视频站
  • 知名网站制作公南充建设机械网站
  • 网站建设实践鉴定微商小程序制作
  • 盗用别人网站图做网站快速排名优化推广手机