producer-consumer

C# Producer-Consumer using Semaphores

不羁岁月 提交于 2021-02-11 10:24:27
问题 Inspired by The Little Book of Semaphores, I decided to implement the Producer-Consumer problem using Semaphores. I specifically want to be able to stop all Worker threads at will. I've tested my methodolodgy extensively and can't find anything faulty. Following code is a prototype for testing and can be ran as a Console application: using System; using System.Collections.Concurrent; using System.Threading; using NUnit.Framework; public class ProducerConsumer { private static readonly int

Threads producer consumer in java

给你一囗甜甜゛ 提交于 2021-02-10 15:46:48
问题 Below is the consumer producer problem code, but the code is not working as expected. Here the consumer and producer are supposed to be just producing and consuming one object. public class ProducerConsumer { private static LinkedList<Integer> linkedList = new LinkedList<>(); public static void main(String a[]) throws InterruptedException { Thread producer = new Thread(new Runnable() { @Override public void run() { synchronized(this) { while (linkedList.size() == 1) { try { wait(); } catch

Why is this multithreaded program getting stuck at infinite loop?

喜夏-厌秋 提交于 2021-02-10 14:53:17
问题 The below program is a simple threaded program . For some reason which i am not able to figure , its getting stuck at infinite loop of both produce() and consume() methods simultaneously in both the threads. It produces the output for a few times and then there is no output at the console. So I presume its getting stuck at the loop. My question is , since the loop depends on the value of the flag valueSet of the same object of Item class , valueSet can't be both true and false at the same

Are SQL Server sequences thread safe?

戏子无情 提交于 2021-02-09 02:28:54
问题 Title is too broad but I couldn't find a more specific one, please feel free to change with better one. I have a table which is working with sequences instead identity. I have three producer applications which are concurrently insert into table, and a consumer application select from table whose status are not processed and then process them and finally update rows as processed. Consumer application has a rule that it does not process the row whose id ( identity column value ) is smaller than

Are SQL Server sequences thread safe?

此生再无相见时 提交于 2021-02-09 02:27:46
问题 Title is too broad but I couldn't find a more specific one, please feel free to change with better one. I have a table which is working with sequences instead identity. I have three producer applications which are concurrently insert into table, and a consumer application select from table whose status are not processed and then process them and finally update rows as processed. Consumer application has a rule that it does not process the row whose id ( identity column value ) is smaller than

Are SQL Server sequences thread safe?

戏子无情 提交于 2021-02-09 02:27:11
问题 Title is too broad but I couldn't find a more specific one, please feel free to change with better one. I have a table which is working with sequences instead identity. I have three producer applications which are concurrently insert into table, and a consumer application select from table whose status are not processed and then process them and finally update rows as processed. Consumer application has a rule that it does not process the row whose id ( identity column value ) is smaller than

Two waiting threads (producer/consumer) with a shared buffer

余生长醉 提交于 2021-02-07 20:28:44
问题 I am trying to have a bunch of producer threads that wait untill the buffer has room for an item, then it puts items in the buffer while it can, going back to sleep if there is no more room. At the same time there should be a bunch of consumer threads that wait untill there is something in the buffer, then it takes things from buffer while it can, going back to sleep if it's empty. In pseudo code, here's what Iam doing, but all Iam getting is deadlocks. condition_variable cvAdd; condition

Single reader multiple writers with pthreads and locks and without boost

别等时光非礼了梦想. 提交于 2021-02-07 08:58:58
问题 Consider the next piece of code. #include <iostream> #include <vector> #include <map> using namespace std; map<pthread_t,vector<int>> map_vec; vector<pair<pthread_t ,int>> how_much_and_where; pthread_cond_t CV = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void* writer(void* args) { while(*some condition*) { int howMuchPush = (rand() % 5) + 1; for (int i = 0; i < howMuchPush; ++i) { // WRITE map_vec[pthread_self()].push_back(rand() % 10); } how_much_and_where

Single reader multiple writers with pthreads and locks and without boost

早过忘川 提交于 2021-02-07 08:57:21
问题 Consider the next piece of code. #include <iostream> #include <vector> #include <map> using namespace std; map<pthread_t,vector<int>> map_vec; vector<pair<pthread_t ,int>> how_much_and_where; pthread_cond_t CV = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void* writer(void* args) { while(*some condition*) { int howMuchPush = (rand() % 5) + 1; for (int i = 0; i < howMuchPush; ++i) { // WRITE map_vec[pthread_self()].push_back(rand() % 10); } how_much_and_where

How to convert a for-loop into a producer?

萝らか妹 提交于 2021-01-29 13:16:39
问题 There is a SynchronousProducer interface, that supports two operations: public interface SynchronousProducer<ITEM> { /** * Produces the next item. * * @return produced item */ ITEM next(); /** * Tells if there are more items available. * * @return true if there is more items, false otherwise */ boolean hasNext(); } Consumer asks the producer if there are more items available and if none goes into a shutdown sequence. Now follows the issue. At the moment there is a for-loop cycle that acts as