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

什么网站做微信公众账号百度浏览器极速版

什么网站做微信公众账号,百度浏览器极速版,买房,松原市城乡建设局网站条件变量 条件变量是一个对象,能够阻止调用线程,直到通知恢复。 std::condition_variable 是 C 标准库中的一个同步原语,它与互斥锁(std::mutex)配合使用,用于线程间的等待和通知机制。 成员函数 wait(…

条件变量

条件变量是一个对象,能够阻止调用线程,直到通知恢复。

std::condition_variable 是 C++ 标准库中的一个同步原语,它与互斥锁(std::mutex)配合使用,用于线程间的等待和通知机制。

成员函数

  • wait():使当前线程阻塞,直到被其他线程唤醒。在调用 wait() 时,线程会自动释放持有的互斥锁,当被唤醒后,线程会重新获取互斥锁。
  • wait_for():使当前线程阻塞一段时间,若在这段时间内被唤醒,则继续执行;若超时仍未被唤醒,则自动返回。
  • wait_until():使当前线程阻塞直到指定的时间点,若在该时间点之前被唤醒,则继续执行;若到达时间点仍未被唤醒,则自动返回。
  • notify_one():唤醒一个等待在该条件变量上的线程。
  • notify_all():唤醒所有等待在该条件变量上的线程。

wait()

无条件:void wait (unique_lock<mutex>& lck);

谓词:template <class Predicate> void wait (unique_lock<mutex>& lck, Predicate pred);

Wait until notified

当前线程(应已锁定 的互斥锁)的执行将被阻止,直到收到通知。

在阻塞线程的那一刻,该函数会自动调用 ,允许其他锁定的线程继续。

一旦收到通知(显式地,由其他线程通知),该函数就会解除阻塞并调用 ,离开时的状态与调用函数时的状态相同。然后函数返回(请注意,最后一个互斥锁可能会在返回之前再次阻塞线程)。 

通常,通过在另一个线程中调用 notify_one 或 notify_all 来通知函数唤醒。但某些 implementations 可能会在不调用这些函数的情况下产生虚假的唤醒调用。因此,此功能的用户应确保满足其恢复条件。

参数

lck:一个unique_lock对象并且当前线程已经被锁住了
对此对象的 wait 成员函数的所有并发调用都应使用相同的底层互斥对象(如 返回的lck.muetx() )。

pred:一个可调用对象或函数,它不接受任何参数并返回一个可以视为为 bool的值
将重复调用此函数,直到其计算结果为<bool>true。

wait_for()

无条件:template <class Rep, class Period> cv_status wait_for (unique_lock<mutex>& lck, const chrono::duration<Rep,Period>& rel_time);

谓词:template <class Rep, class Period, class Predicate> bool wait_for (unique_lock<mutex>& lck, const chrono::duration<Rep,Period>& rel_time, Predicate pred);

Wait for timeout or until notified

当前线程(应已锁定 的互斥锁)的执行将在rel_time内被阻止,或者提前收到通知则解除。

在阻塞线程的那一刻,该函数会自动调用 ,允许其他锁定的线程继续。

一旦收到通知或者rel_time已过,该函数就会解除阻塞并调用,离开时的状态与调用函数时的状态相同。然后函数返回(请注意,最后一个互斥锁可能会在返回之前再次阻塞线程)。 

通常,通过在另一个线程中调用 notify_one 或 notify_all 来通知函数唤醒。但某些 implementations 可能会在不调用这些函数的情况下产生虚假的唤醒调用。因此,此功能的用户应确保满足其恢复条件。

参数

lck:一个unique_lock对象并且当前线程已经被锁住了
对此对象的 wait 成员函数的所有并发调用都应使用相同的底层互斥对象(如 返回的lck.muetx() )。

rul_time:线程将阻塞等待通知的最大时间跨度。

duation是表示特定相对时间的对象

pred:一个可调用对象或函数,它不接受任何参数并返回一个可以视为为 bool的值
将重复调用此函数,直到其计算结果为<bool>true。

返回值

1)无条件:(1)如果函数因rel_time时间过去而返回,则返回cv_status::timeout,否则返回cv_status::no_timeout

2)谓词:无论超时是否被触发,都返回pred()(不过如果触发了超时,它只能返回false

wait_until()

无条件:template <class Clock, class Duration> cv_status wait_until (unique_lock<mutex>& lck, const chrono::time_point<Clock,Duration>& abs_time);

谓词:template <class Clock, class Duration, class Predicate> bool wait_until (unique_lock<mutex>& lck, const chrono::time_point<Clock,Duration>& abs_time, Predicate pred);

Wait until notified or time point

当前线程(应已锁定的互斥锁)的执行将被阻止,直到通知或直到abs_time ,以先发生者为准。 

在阻塞线程的那一刻,该函数会自动调用 ,允许其他锁定的线程继续。

一旦收到通知或者到了abs_time,该函数就会解除阻塞并调用,离开时的状态与调用函数时的状态相同。然后函数返回(请注意,最后一个互斥锁可能会在返回之前再次阻塞线程)。

通常,通过在另一个线程中调用 notify_one 或 notify_all 来通知函数唤醒。但某些 implementations 可能会在不调用这些函数的情况下产生虚假的唤醒调用。因此,此功能的用户应确保满足其恢复条件。 

参数

lck:一个unique_lock对象并且当前线程已经被锁住了
对此对象的 wait 成员函数的所有并发调用都应使用相同的底层互斥对象(如 返回的lck.muetx() )。

abs_time:线程将停止阻塞以允许函数返回的时间点。

tme_point是表示特定绝对时间的对象。

pred:一个可调用对象或函数,它不接受任何参数并返回一个可以视为为 bool的值
将重复调用此函数,直到其计算结果为<bool>true。

返回值

1)无条件:如果函数因已达到绝对时间abs_time而返回, 则返回cv_status::timeout,否则返回ccv_status::no_timeout

2)谓词:无论超时是否被触发,都会返回pred()的值(不过只有在超时被触发的情况下,该返回值才可能为假)。

notify_one()

void notify_one() noexcept;

Notify one

解除当前正等待此条件的线程中的一个线程的阻塞状态。

如果没有线程在等待,该函数不执行任何操作。

如果有多个线程在等待,那么具体选择唤醒哪个线程是未指定的。

参数:无

返回值:无

notify_all()

void notify_all() noexcept;

Notify all

解除当前所有正等待此条件的线程的阻塞状态。

如果没有线程在等待,该函数不执行任何操作。

参数:无

返回值:无

 

Example

//condition_variable example
#include<iostream>  //std::cout
#include<thread>  //std::thread
#include<mutex>  //std::mutex,std::unique_lock
#include<windows.h>  //Sleep
#include<condition_variable>  //std::condition_variablestd::mutex mtx;
std::condition_variable cv;
bool ready = false;void print_id(int id) {std::unique_lock<std::mutex> lck(mtx);while (!ready) {std::cout << "Waiting: " << id << std::endl;cv.wait(lck);}//...std::cout << "thread " << id << std::endl;
}void go() {std::unique_lock<std::mutex> lck(mtx);ready = true;cv.notify_all();
}int main() {std::thread threads[10];// spawn 10 threads:for (int i = 0; i < 10; i++) {threads[i] = std::thread(print_id, i);}std::cout << "10 threads ready to race..." << std::endl;Sleep(2000);go();for (auto& th : threads) {th.join();}return 0;}
// condition_variable::wait (with predicate)
#include<iostream>  //std::cout
#include<thread>  //std::thread,std::this_thread::yield()
#include<mutex>  //std::mutex,std::unique_lock
#include<condition_variable>  //std::condition_variablestd::mutex mtx;
std::condition_variable cv;int cargo = 0;
bool ship_avaiable() {return cargo != 0;
}void consume(int n) {for (int i = 0; i < n; ++i) {std::unique_lock<std::mutex> lck(mtx);cv.wait(lck,ship_avaiable);//consumestd::cout << cargo <<'\n';cargo = 0;}
}
int main() {std::thread consumer_thread(consume, 10);//produce 10 items when neededfor (int i = 0; i < 10; i++) {while (ship_avaiable()) { std::this_thread::yield(); }std::unique_lock<std::mutex> lck(mtx);cargo = i + 1;cv.notify_one();}consumer_thread.join();return 0;
}
// condition_variable::wait_for example
#include<iostream>  //std::cout
#include<thread>  //stdh::thread
#include <chrono>  //std::chrono::seconds
#include<mutex>  //std::mutex,std::unique_lock
#include<condition_variable>  //std::condition_variabel,std::cv_statusstd::condition_variable cv;int value;void read_vaule() {std::cin >> value;cv.notify_one();
}int main() {std::cout << "Please, enter an integer (I'll be printing dots): \n";std::thread th(read_vaule);std::mutex mtx;std::unique_lock<std::mutex> lck(mtx);while (cv.wait_for(lck, std::chrono::seconds(1)) == std::cv_status::timeout) {std::cout << '.' << std::endl;}std::cout << "You entered: " << value << std::endl;th.join();return 0;
}
// condition_variable::notify_one
#include<iostream>  //std::cout
#include<mutex>  //std::mutex,std::unique_lock
#include<thread>  //std::thread
#include<condition_variable>  //std::condition_variablestd::mutex mtx;
std::condition_variable produce,consume;int cargo = 0;  //shared vaule by producers and consumervoid consumer() {std::unique_lock<std::mutex> lck(mtx);while (cargo == 0) {consume.wait(lck);}std::cout << cargo << '\n';cargo = 0;produce.notify_one();
}void producer(int id) {std::unique_lock<std::mutex> lck(mtx);while (cargo != 0) {produce.wait(lck);}cargo = id;consume.notify_one();
}
int main() {std::thread producers[10], consumers[10];//spawn 10 producers and 10 consumersfor (int i = 0; i < 10; ++i) {consumers[i] = std::thread(consumer);producers[i] = std::thread(producer,i+1);}// join them back:for (int i = 0; i < 10; ++i) {producers[i].join();consumers[i].join();}return 0;
}

 

// condition_variable::notify_all
#include <iostream>           // std::cout
#include <thread>             // std::thread
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variablestd::mutex mtx;
std::condition_variable cv;
bool ready = false;void print_id (int id) {std::unique_lock<std::mutex> lck(mtx);while (!ready) cv.wait(lck);// ...std::cout << "thread " << id << '\n';
}void go() {std::unique_lock<std::mutex> lck(mtx);ready = true;cv.notify_all();
}int main ()
{std::thread threads[10];// spawn 10 threads:for (int i=0; i<10; ++i)threads[i] = std::thread(print_id,i);std::cout << "10 threads ready to race...\n";go();                       // go!for (auto& th : threads) th.join();return 0;
}


文章转载自:

http://q0ANLVA4.dyrzm.cn
http://y47xbgxd.dyrzm.cn
http://jmjitl6Z.dyrzm.cn
http://IUKZ8CFM.dyrzm.cn
http://guKeb19y.dyrzm.cn
http://BMKyolBZ.dyrzm.cn
http://rlAZUhS5.dyrzm.cn
http://z5BqM9MG.dyrzm.cn
http://iRtJ7JPo.dyrzm.cn
http://4hQmz3rq.dyrzm.cn
http://gr9ssNKM.dyrzm.cn
http://F0hwKu6Z.dyrzm.cn
http://vT5BN8Tv.dyrzm.cn
http://KH5p3OLr.dyrzm.cn
http://tXoBsW8h.dyrzm.cn
http://TimL7x4W.dyrzm.cn
http://asWkdPBp.dyrzm.cn
http://lW500NID.dyrzm.cn
http://MfYmpGGM.dyrzm.cn
http://Aru2zSsf.dyrzm.cn
http://pMeV9iec.dyrzm.cn
http://4in7dNzb.dyrzm.cn
http://r1iuw4HQ.dyrzm.cn
http://uBr9nSI9.dyrzm.cn
http://qRVqX1Rm.dyrzm.cn
http://DwFDYmD5.dyrzm.cn
http://hF4Y3SsD.dyrzm.cn
http://auqBsqiT.dyrzm.cn
http://v0SSYEo0.dyrzm.cn
http://hXCBQGJW.dyrzm.cn
http://www.dtcms.com/wzjs/733091.html

相关文章:

  • 成都个人网站建设在网站做登记表备案 如果修改
  • 手机在网上怎么创建自己的网站哪儿有做字体设计的网站
  • 手机网站做seo做渔家乐哪个网站最好
  • 网站特点怎么写php双语网站源码
  • 产品seo是什么意思六安网站自然排名优化价格
  • 汽车网站营销品牌网站建设小蝌蚪a
  • 做暧嗳xo小视频免费网站网站如何转做app
  • 百度免费资源网站电子商务网站建设参考文献
  • 申请免费个人网站app的技术框架有哪些
  • 大连网龙建站优化推广河南省副厅长
  • 仕德伟做的网站图片怎么修长沙好的互联网公司
  • 保定商城网站建设网站建设推进表
  • 阿里巴巴做短视频网站网站很久没被收录的新闻怎么处理
  • 湖北城乡建设部网站首页页面模板在公号什么地方显示
  • wordpress手机站郑州做网站公司汉狮网
  • 房地产微网站模板wordpress 后台禁止谷歌字体
  • 做网站的公司叫中什么深入解析wordpress二手
  • 写作网站都有哪些ppp怎样看网站的建设时间
  • 一流的龙岗网站制作网加商学院网站怎么做
  • elementui 做的网站公众号排版怎么做
  • 网站经营与建设自媒体平台注册
  • 安阳哪有做网站的西安市建网站找哪家
  • 沈阳哪家网站做的好想学软件开发报什么专业
  • 网站建设体会心得建个简单网站
  • iis 7.0 搭建网站重庆那家做网站做得好
  • 做网站国内阿里云虚拟主机多少钱硬件开发是什么
  • 门户网站建设重要性好看的网站推荐一下
  • 建设银行山西招聘网站seo5
  • 工商网站wordpress获取热门文章
  • 个人在网站怎么做工作需要原则和最小化原则是确定国家秘密知悉范围