semaphore

Implementing a cyclicbarrier in java using semaphores

走远了吗. 提交于 2019-12-25 02:50:28
问题 The question is as follows, since the barrier is only called using down() so that it would wait for the n threads to arrive and then execute all n threads together in the critical region now how do I inform the threads calling on barrier.down that it can move on now. I tried adding notifyAll() after phase2() and that doesn't work. Help? :) public class cyclicBarrier { private int n; private int count; private semaphore mutex; private semaphore turnstile; private semaphore turnstile2; public

Resume a program if variable's value changes

二次信任 提交于 2019-12-25 02:06:20
问题 Is there a way in c / c++ to pause a program (might be with SIGSTOP ), and then to continue ( SIGCONT ?) it when another thread changes a value of some variable? For example: int main(void) { int a = 0; createThread(&a); //creates a thread which will change a's value pauseMyself(); //pauses main process till a's value is changed /* main process gets here only when a's value has been changed */ fprintf(stderr, "a's value has been changed\n"); return 0; } I want to see if it is possible so I

client get segmentation fault when attach to shared memory

十年热恋 提交于 2019-12-25 01:39:43
问题 Hi i want to implement a client-server program that communicates with each other via shared memory. in the server side i have two threads. a writer thread and a reader thread. the writer thread puts some data into the queue and the reader thread reads from it and passes the data to the client side via shared memory.each client that has shmid can attach itself to it and read data from it... right now i get segmentation fault at client side when it comes to attach to shared memory. here is my

Controlled Parallel task execution what to use

末鹿安然 提交于 2019-12-24 20:52:52
问题 I'm running parallel tasks over some objects, to make this faster they run 4 in parallel. Now there is a new requirement, I need to perform a task that requires to be executed one at a time while the others must be waiting (the others cannot be performing another task). I can't make the tests Sync. Please check the image bellow as a better explanation. Executing one at a time is done, but I thought of using a Semaphore to wait for the 4 objects in the beginning and at the end. The problem is

Share named POSIX semaphores

别等时光非礼了梦想. 提交于 2019-12-24 20:40:11
问题 I have issues understanding how to share a POSIX semaphore among multiple processes. I am trying to do the following: 1. The producer initializes a semaphore 2. The producer posts 10 tokens to the semaphore and sleeps 1 second before doing so 3. The consumer gets a token from the semaphore When I start my producer, a segmentation fault (core dumped) occurs. Furthermore I am not sure, if my way of sharing the named semaphore is correct. Producer: #include <semaphore.h> #include <stdio.h>

Reusable barrier implementation using POSIX semaphores

纵然是瞬间 提交于 2019-12-24 20:37:03
问题 Need a solution that creates 5 pthreads. Each pthread executes a function that involves iterating through a loop 10 times. In each iteration of the loop, a thread increments an int from 0 to 0.9*MAX_INT and then prints the iteration number. Make sure that each of the 5 threads finish the ith iteration of the loop before they can start the (i+1)th iteration (i.e. all threads synchronize/rendezvous towards the end of each iteration). I need to use a two-phase barrier implemented using POSIX

Indexing into an array of semaphores

半腔热情 提交于 2019-12-24 17:23:32
问题 Lets say I initialized two global arrays of semaphores, semaphore empty[someNum]; and semaphore full[someNum]; and someNum is initialized as const int someNum = 3; (globally) I'll have a method called init() and inside is a for-loop to help index those arrays. for (index=0; index<someNum; index++) num[index]=0; my goal is to use commands like wait and signal certain semaphores in the array, for example if num is full, then I do not want my producer to place a value into it. inside of init() I

One thread showing interest in another thread (consumer / producer)

冷暖自知 提交于 2019-12-24 12:38:15
问题 I would like to have to possibility to make thread (consumer) express interest in when another thread (producer) makes something. But not all the time. Basically I want to make a one-shot consumer. Ideally the producer through would go merrily about its business until one (or many) consumers signal that they want something, in which case the producer would push some data into a variable and signal that it has done so. The consumer will wait until the variable has become filled. It must also

Inno Setup Semaphore non GUI Blocking

核能气质少年 提交于 2019-12-24 10:48:49
问题 I have an Inno Setup script which calls a DLL. The DLL starts a thread which in the end calls a function pointer in the Inno Setup. Since i don't want to change the logic of my Inno Script to much i would like to use a semaphore or something like that. The important part in here is that the gui shouldn't be blocked. here a little snippet of my code procedure InstallData(); var arrComponents : TStringList; i, index, p : Integer; countComp : Integer; begin countComp := ICountComponents();

Testing a semaphore by counting

对着背影说爱祢 提交于 2019-12-24 09:39:58
问题 There’s an article about semaphores on OS X. The author tests the semaphore by incrementing and decrementing a static variable in two threads. With semaphore guarding the variable access, the variable ends up being zero. Without the guard the variable ends up having some bogus value. I tried the code and it works. What I don’t understand is how may the concurrent access from the two threads make a difference in the final variable value. After all it seems to me like a bunch of +1s and –1s