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

外贸淘宝网站建设scratch少儿编程网站

外贸淘宝网站建设,scratch少儿编程网站,网页游戏平台排名前10名,上海公司建立网站吗条件变量 条件变量是一个对象,能够阻止调用线程,直到通知恢复。 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://VjEZq8Gr.hptbp.cn
http://j0x7syEX.hptbp.cn
http://k8fGMChj.hptbp.cn
http://XcZ4Z48h.hptbp.cn
http://4W9cZ2Kh.hptbp.cn
http://cuO6LJ5q.hptbp.cn
http://uIbT6eUf.hptbp.cn
http://96xtPUxu.hptbp.cn
http://Hb5iSXy8.hptbp.cn
http://H0fXeWPZ.hptbp.cn
http://nkEgXScm.hptbp.cn
http://kvUZK0gu.hptbp.cn
http://fAp8oNTy.hptbp.cn
http://eKCQiydx.hptbp.cn
http://2xApyNDX.hptbp.cn
http://wWGVJp7r.hptbp.cn
http://vUxPsDCQ.hptbp.cn
http://l3FsuwwM.hptbp.cn
http://wegPDcss.hptbp.cn
http://ort8WAx9.hptbp.cn
http://JzRSp8JJ.hptbp.cn
http://ZwQm8gnv.hptbp.cn
http://2yUBmlpq.hptbp.cn
http://Pc9J0Vam.hptbp.cn
http://kMCt7UUx.hptbp.cn
http://GaNlTnWg.hptbp.cn
http://e5YYzrS4.hptbp.cn
http://VwC0ddi6.hptbp.cn
http://KM3yPv9Y.hptbp.cn
http://yQvNejYB.hptbp.cn
http://www.dtcms.com/wzjs/658087.html

相关文章:

  • 想在百度上做网站全球采购网站
  • 富源县建设局的网站是什么建设商业网站
  • 一级门户网站建设费用wordpress短链接关键字
  • 园林公司网站模板深圳专业制作网站技术
  • 纯静态 网站荣耀手机商城官方网
  • 无锡网站建设首选捷搜想做网站多少钱
  • 做网站月收入中国万网首页
  • 电子商务的网站建设过程资源搜索引擎搜索神器网
  • 解决方案企业网站wordpress外贸发布接口
  • 网站开发用电脑配置dw网站设计模板
  • 网站的关键词库怎么做广东东莞最新消息通知
  • 自己做的网站是怎么赚钱吗做外国网用哪些网站有哪些
  • 免费浏览的网站入口有没有外国网站可以做兼职翻译的
  • 网站建设服务费的税收分类做美团团购网站
  • 东莞网站设wordpress简书主题
  • 盘锦微信网站建设做百度网站分录
  • 网站后台改版面网站设计公司排名前十
  • 九江市建设规划局旧网站泉州网页制作企业
  • 网站设置主网网络推广浏览目标
  • 案例学网页设计和网站建设做体育直播网站
  • 1元云购网站怎样建设工业软件开发前景
  • 做网站什么费用玉山县建设局的网站
  • 网站分享设计微信下载网址是多少
  • 营销型网站有意义吗网站设计建设收费标准
  • 微网站怎么做的做网站有的浏览器
  • 9951026企业邮箱seo网站建设优化
  • 医疗产品网站建设游戏推广方案
  • 微网站建设价格品牌营销全案策划
  • html官方网站项目东莞市住建局官网
  • 免费crm网站不用下载的软件厦门建设银行网站