C++多线程-第五篇-同步机制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hffhjh111/article/details/53141590

Call_once

使用call_once包装的函数在多线程中只会被执行一次。

Void call_once(once_flag&flag, Callable && func,Args &&.....args);

其中once_flag 声明必须是线程安全的,可采用static.且不可使用临时变量给Call_once.

 

 

条件变量--同步机制

需要和互斥量配合使用。

Thread中含有两种,condition_variable 和 condition_variable_any,

后者更常用并可以适应更广泛的互斥量类型。

类摘要


 
 
  1. Enum class cv_status{ no_timeout, timeout }; //等待状态
  2. Class condition_variable_any
  3. {
  4. Public:
  5. Void notify_one(); //通知一个等待中的线程
  6. Void notify_all(); //通知所有等待
  7. Void wait(lock_type_&lock); //等待
  8. Void wait(lock_type&lock,
  9. predicate_type predicate); //条件等待
  10. Cv_status wait_for(lock_type&lock,
  11. Const duration& d); //等待相对时间
  12. Cv_status wait_for(lock_type&lock,
  13. Const duration&d,
  14. Predicate_type predicate); //条件等待相对时间
  15. Cv_status wait_until(lock_type&lock,
  16. Const time_point & t); //等待绝对时间
  17. Cv_status wait_for(lock_type&lock,
  18. Const time_point & t,
  19. Predicate_type predicate); //条件等待绝对时间
  20. };


Code:(参考某Blog,忘记出处,Sorry)


 
 
  1. #include<iostream>
  2. #include<stack>
  3. #include<boost/thread/thread.hpp>
  4. #include<boost/thread/lock_factories.hpp>
  5. using namespace std;
  6. using namespace boost;
  7. class buffer
  8. {
  9. private:
  10. mutex mu; //配合条件变量
  11. condition_variable_any cond_put;
  12. condition_variable_any cond_get;
  13. stack< int> stk;
  14. int un_read, capacity;
  15. bool is_full()
  16. {
  17. return un_read == capacity;
  18. }
  19. bool is_empty()
  20. {
  21. return un_read == 0;
  22. }
  23. public:
  24. buffer( size_t n) :un_read( 0), capacity(n){}
  25. void put(int x)
  26. {
  27. {
  28. auto lock = make_unique_lock(mu);
  29. for (; is_full();)
  30. {
  31. cout << "full waiting ..." << endl;
  32. cond_put.wait(lock); //条件变量等待
  33. }
  34. stk.push(x);
  35. ++un_read;
  36. }
  37. cond_get.notify_one();
  38. }
  39. void get(int &x)
  40. {
  41. {
  42. auto lock = make_unique_lock(mu);
  43. for (; is_empty();)
  44. {
  45. cout << "empty waiting ..." << endl;
  46. cond_get.wait(lock);
  47. }
  48. --un_read;
  49. x = stk.top();
  50. stk.pop();
  51. }
  52. cond_put.notify_one();
  53. }
  54. };
  55. mutex mux; //输出好看,,,,
  56. buffer buf(5);
  57. void producer(int n)
  58. {
  59. for ( int i = 0; i < n; i++)
  60. {
  61. mux.lock();
  62. cout << "put" << i << endl;
  63. mux.unlock();
  64. buf.put(i);
  65. }
  66. }
  67. void consumer(int n)
  68. {
  69. int x;
  70. for ( int i = 0; i < n; i++)
  71. {
  72. buf.get(x);
  73. mux.lock();
  74. cout << "get" << x << endl;
  75. mux.unlock();
  76. }
  77. }
  78. int main()
  79. {
  80. //条件变量实现生产者与消费者模式
  81. thread_group tg;
  82. tg.create_thread(bind(producer, 20));
  83. tg.create_thread(bind(consumer, 10));
  84. tg.create_thread(bind(consumer, 10));
  85. tg.join_all();
  86. return 0;
  87. }





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值