Suppose we have two workers. Each worker has an id of 0 and 1. Also suppose that we have jobs arriving all the time, each job has also an identifier 0 or 1 which specifies which worker will have to do this job.

I would like to create 2 threads that are initially locked, and then when two jobs arrive, unlock them, each of them does their job and then lock them again until other jobs arrive.

I have the following code:

  #include <iostream>
#include <thread>
#include <mutex> using namespace std; struct job{ thread jobThread;
mutex jobMutex; }; job jobs[2]; void executeJob(int worker){ while(true){ jobs[worker].jobMutex.lock(); //do some job } } void initialize(){ int i;
for(i=0;i<2;i++){
jobs[i].jobThread = thread(executeJob, i);
} } int main(void){ //initialization
initialize(); int buffer[2];
int bufferSize = 0; while(true){
//jobs arrive here constantly,
//once the buffer becomes full,
//we unlock the threads(workers) and they start working
bufferSize = 2;
if(bufferSize == 2){
for(int i = 0; i<2; i++){
jobs[i].jobMutex.unlock();
}
}
break;
} }

I started using std::thread a few days ago and I'm not sure why but Visual Studio gives me an error saying abort() has been called. I believe there's something missing however due to my ignorance I can't figure out what.

I would expect this piece of code to actually

  1. Initialize the two threads and then lock them

  2. Inside the main function unlock the two threads, the two threads will do their job(in this case nothing) and then they will become locked again.

But it gives me an error instead. What am I doing wrong?

Thank you in advance!

最新文章

  1. 开始研究web,mark一下
  2. Swift方法实现回调的一种方式
  3. 完全卸载VS2005或VS2008的步骤
  4. 训练集(train set) 验证集(validation set) 测试集(test set)
  5. 数往知来 ADO.NET &lt;八&gt;
  6. CSS的引入方式
  7. JQUERY1.9学习笔记 之基本过滤器(八) 最后元素选择器
  8. ThinkPHP - 自动创建 + 自动验证 + 自动完成
  9. [DevEpxress]GridControl 显示Gif动画
  10. poj 3253 Fence Repair 优先队列
  11. SOA专题---Dropwizard与Spring Boot比较
  12. Kubernetes — 我的第一个容器化应用
  13. luogu P1064|| 01背包||金明的预算
  14. win 10安装Linux虚拟机教程
  15. centos6安装tomcat8.5
  16. Tensorflow手写数字识别---MNIST
  17. 尚学堂java答案解析 第二章
  18. 04_web基础(五)之cookie与session
  19. RPDU
  20. ElasticSearch(六):IK分词器的安装与使用IK分词器创建索引

热门文章

  1. asp.net MVC Session 第二次加载丢失问题
  2. golang协程——通道channel阻塞
  3. HDU_6017_Girls love 233_(dp)(记忆化搜索)
  4. 再谈布局之 UIStackView
  5. Windows提高_2.2第二部分:用户区同步
  6. Vue-prop
  7. JavaScript中的方法
  8. php第二十二节课
  9. JAVA之文件操作1,2,3,4
  10. highcharts图表的常见操作