semaphore

Find number of tasks blocking on a POSIX semaphore

时光毁灭记忆、已成空白 提交于 2019-12-13 21:34:39
问题 Is there any way by which I can know the number of processes or threads waiting on a particular semaphore? Like a API to check the value. Sem_getvalue() only returns 0 and not a negative number whose absolute value is the number of tasks blocking on the semaphore as mentioned on a few sites. Any help would be great. Thanks in advance!! 回答1: There is no way to do this in the POSIX API other than sem_getvalue , which semantics, as you have seen, are optional. That said, Linux implements named

Error while compiling the code 'double free or corruption (out)' using threads in C? [closed]

喜你入骨 提交于 2019-12-13 09:55:19
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I'm trying to do a ebola simulation using pthreads. Everything worked just fine up to the semaphor part. I'm getting this error while compiling code: *** Error in `./ebola_serial': double free or corruption (out): 0x00007f49700008c0 *** *** Error in `======= Backtrace: =========

Producer/Consumer using Semaphore; getting deadlock

风流意气都作罢 提交于 2019-12-13 06:12:33
问题 According to http://en.wikipedia.org/wiki/Producer-consumer_problem I want to simulate P/C problem using semaphore. I am getting deadlock and I don't know what is problem. public static void main(String[] args) { CustomBlockingQueue blockingQueue = new CustomBlockingQueue(); new Thread(new Producer(blockingQueue)).start(); new Thread(new Consumer(blockingQueue)).start(); } } @SuppressWarnings("serial") class CustomBlockingQueue extends LinkedList<Object> { private static final int MAX_SIZE =

Barrier implementation for inter process in shared memory

随声附和 提交于 2019-12-13 04:59:19
问题 I am looking for an inter-processes barrier implementation. Processes are in shared memory (ie. on the same ndoe). Processes are MPI ones. I do not want to use the MPI_Barrier function, because the general policy for all the MPI implementation is active waiting. I want my processes sleeping as long as they wait. The restrictions: should be in C, maybe in C++ no spinlock, so it could use semaphore linux OS I am confident it exists thousands of barrier implementation, but I do not find any?!

Second thread doesn't start in producer consumer example

人盡茶涼 提交于 2019-12-13 03:57:51
问题 I'm trying to implement Producer and Consumer problem by using semaphores in java. The issue is when I start two threads (Producer and Consumer) consumer doesn't start and producer blocks itself after buffer is full. I mean it looks like there is only one thread which works in synchronous manner. Thus, as I mentioned I use 3 semaphores which are empty, full, and mutex. Here is the simplest code; Producer class; import java.util.concurrent.Semaphore; public class Producer implements Runnable {

Java 并发(JUC 包-05)

百般思念 提交于 2019-12-13 03:56:45
>同步工具 Semaphore 这个类代表一个计数信号量。可以控制某个资源可被同时访问的个数,acquire() 获得一个许可,如果没有就等待,release() 释放一个许可。Semaphore维护了当前访问的个数,提供同步访问机制,控制同时访问的个数。 例子:控制线程两个两个的运行 public class TestSemaphore { public static void main(String[] args) { new TestSemaphore().go(); } public void go() { // 这就像是一个“许可”“信号灯” Semaphore semaphore = new Semaphore(2); Dosth dosth = new Dosth(); for (int i = 0; i < 10; i++) { new Thread(new Runnable() { @Override public void run() { try { semaphore.acquire(); dosth.work(); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } } }, "线程" + i).start(); } } class Dosth {

CountDownLatch、Semaphore等4大并发工具类详解

送分小仙女□ 提交于 2019-12-12 22:30:07
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Java并发工具包 1.并发工具类 提供了比synchronized更加高级的各种同步结构:包括CountDownLatch、CyclicBarrier、Semaphore等,可以实现更加丰富的多线程操作。 2.并发容器 提供各种线程安全的容器:最常见的ConcurrentHashMap、有序的ConcurrentSkipListMap,实现线程安全的动态数组CopyOnWriteArrayList等。 3.并发队列 各种BlockingQueue的实现:常用的ArrayBlockingQueue、SynchorousQueue或针对特定场景的PriorityBlockingQueue。 4.Executor框架 可以创建各种不同类型的线程池,调度任务运行等,绝大部分情况下,不再需要自己从头实现线程池和任务调度器。 Java常用的并发容器 1.ConcurrentHashMap 经常使用的并发容器,JDK 1.7和1.8的底层数据结构发生了变化(后续文章会详解),这里可以建议学习顺序如下:从Java7 HashMap -> Java7 ConcurrentHashMap -> Java8 HashMap -> Java8 ConcurrentHashMap,这样可以更好的掌握这个并发容器

Looking for good analogy/examples for monitor verses semaphore

北城以北 提交于 2019-12-12 16:25:38
问题 A monitor is supposed to solve problems with semaphores in concurrent environments. I'm looking for a good analogy using a monitor verses semaphore. Please use information for the analogy: 4 tasks (TaskA, TaskB, TaskC, TaskD) 1 variable varX Each Task wants to manipulate varX based on some event. 回答1: Lets say a bunch of patients wants to go see a doctor. A semaphore implementation would be they all stand outside the door to the office, as soon as one patient comes out, they all try to

Semaphores in .NET compact framework

℡╲_俬逩灬. 提交于 2019-12-12 16:15:19
问题 Unfortunately, there is no Semaphore in System.Threading when using the .NET Compact Framework. I'm not sure why that is, does anyone have an idea? After googling I've found a bunch of people giving their own implementations, but none of them really worked great... or at all! So I've come to ask the experts... Does anyone have a good semaphore class/library they can recommend for the .NET compact framework? OR Is there someway I can emulate the behaviour? I have a typical producer/consumer

Semaphores makeWater() synchronization

為{幸葍}努か 提交于 2019-12-12 14:07:53
问题 This program claims to solve makeWater() synchronization problem. However, I could not understand how. I am new in semaphores. I would appriciate if you can help me to understand this code. 回答1: So you need to make H2O (2Hs and one O) combinations out of number of simultaneously running H-threads and O-threads. The thing is one 'O' needs two 'H' s. And no sharings between two different water molecules. So assume number of O and H threads start their processes. No O thread can go beyond P(o