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!

C/C++ Threads): Creating worker threads that will be listening to jobs and executing them concurrently when wanted的更多相关文章

  1. 解决 APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tas

    报错信息:APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks! 在网上查了一下,大部分网友分析是c ...

  2. eclipse run on server 时 报的错误APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!

    写这篇日记记录一下自己的愚蠢行为. 具体报错如下: 信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ ...

  3. c3p0 APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks

    2018-01-04 15:02:03,319 ---com.mchange.v2.async.ThreadPoolAsynchronousRunner: com.mchange.v2.async.T ...

  4. 【SQLServer】max worker threads参数说明

    本文介绍如何使用SQL Server Management Studio或Transact-SQL在SQL Server中配置最大工作线程服务器配置选项. max worker threads选项配置 ...

  5. 【SQLServer】max worker threads参数配置

    查看和设置max worker threads USE master; //选中你想设置max worker threads的数据库.master表示在实例级别进行设置 GO EXEC sp_conf ...

  6. Node.js躬行记(23)——Worker threads

    Node.js 官方提供了 Cluster 和 Child process 创建子进程,通过 Worker threads 模块创建子线程.但前者无法共享内存,通信必须使用 JSON 格式,有一定的局 ...

  7. 【C#】转一篇MSDN杂志文:ASP.NET Pipeline: Use Threads and Build Asynchronous Handlers in Your Server-Side Web Code

    序:这是一篇发表在2003年6月刊的MSDN Magazine的文章,现在已经不能在线阅读,只提供chm下载.讲的是异步请求处理那些事,正是我上一篇博文涉及的东西(BTW,事实上这篇杂志阐述了那么搞然 ...

  8. Worker Thread

    http://www.codeproject.com/Articles/552/Using-Worker-Threads Introduction Worker threads are an eleg ...

  9. ThreadPoolExecutor – Java Thread Pool Example

    https://www.journaldev.com/1069/threadpoolexecutor-java-thread-pool-example-executorservice   Java t ...

随机推荐

  1. Spark性能优化指南-高级篇(spark shuffle)

    Spark性能优化指南-高级篇(spark shuffle) 非常好的讲解

  2. WordPress百度熊掌号页面改造(纯代码实现)

    一.粉丝关注改造 1.添加熊掌号ID声明 ID声明 <script src="//msite.baidu.com/sdk/c.js?appid=你的熊掌ID">< ...

  3. Js变量类型

    值类型和引用类型 值类型(基本类型):5种,Number String Boolean null undefined var a=10; var b=a; a=2; console.log(b); a ...

  4. Django基础之数据库增删改查

    Django中生成多个APP,每个APP下都有自己models模块,避免了多个APP之间数据的相互影响. 1.首先在APP的models下创建一个类 class UserInfo(models.Mod ...

  5. CommHelper

    18位流水号: public static string GenerateTransId(int i) { string transId = DateTime.Now.ToString("y ...

  6. 使用openssl搭建CA并颁发服务器证书

    本来整理了一份执行脚本,但是没有找到附件功能.只好直接贴当时自己看过的链接了. 文章标题:Openssl Certificate Authority 转载链接:https://jamielinux.c ...

  7. 3星|《投机教父尼德霍夫的股票投机术》:2003年的书了。作者97年投机大亏后在CNBC《金钱》栏目上的股市评论文章集。

    查资料作者在97年金融危机中大亏,之后在CNBC<金钱>栏目上跟人合写股市评论文章,本书是那些股评文章的集合.有资料说作者在08年有一次大亏. 从这些文章看,作者是比较冷静地看待股市的,不 ...

  8. 树莓派安装CentOS

    1.下载并安装,这里使用的是 centos系统地址:http://mirror.centos.org/altarch/7/isos/armhfp/ 下载CentOS-Userland-7-armv7h ...

  9. JMeter在linux上分布式压测环境配置(一)

    环境配置 一.在Linux服务器先安装SDK 1.先从官网下载jdk1.8.0_131.tar.gz,l(linux版本,32位,64位根据系统来判断) 2.在/usr/目录下创建java文件夹,(当 ...

  10. RabbitMQ系列(四)--消息如何保证可靠性传输以及幂等性

    一.消息如何保证可靠性传输 1.1.可能出现消息丢失的情况 1.Producer在把Message发送Broker的过程中,因为网络问题等发生丢失,或者Message到了Broker,但是出了问题,没 ...