FAQ Database Discussion Community


Thread blocking on sem_wait causes other threads to hang

c,multithreading,pthreads,semaphore,fifo
I wrote a generic queue in C which is to be used for a variety of payload types. It is a blocking queue so that consumer threads will block waiting for the queue to be populated by producer threads. I have tested the queue code in isolation using check, including...

Semaphore value initialized to -1?

c,ipc,semaphore
I am trying to initialize a binary semaphore for some shared memory. I could not figure out why I hung on a semop to get the semaphore, and eventually found that the semaphore value was -1 and I was stuck waiting. What I don't get is why the semaphore value...

Using volatile variables and semaphores - Java

java,multithreading,mutex,semaphore,volatile
I'm starting with Threads, Semaphores, volatile variables, etc. I wonder if when I'm using Semaphores it is necessary to define the variable as volatile, I mean: Having 2 Threads, one increases and the other decreases the variable for example and obviously, before each access I have a mutex that controls...

Synchronizing processes with semaphores and signals in C

c,linux,multiprocessing,signals,semaphore
I have to write program in C on Linux. It has to have 3 processes - first reads from STDIN, sends message through FIFO to second process, which counts lenght of recevied message and sends result to third process (also through FIFO), which displays it on STDOUT. I have to...

WKWebView: trying to query javascript synchronously from the main thread

ios,ios8,grand-central-dispatch,semaphore,wkwebview
Is there any way to query the javascript synchronously from the main thread? Javascript is queried from the native code using an asynchronous function with a callback parameter to handle the response: func evaluateJavaScript(_ javaScriptString: String, completionHandler completionHandler: ((AnyObject!, NSError!) -> Void)?) Asynchronous behavior can usually be turned synchronous by...

Where should a singleton mutex be released in an IDisposable derived class?

c#,concurrency,mutex,semaphore,idisposable
Background I am working on implementing a derived class of a database accessor that should only allow one thread at a time access to the database. But this question should be applicable to any form of single thread access with the Disposable pattern. In Stephen Cleary's blog he shows how...

Semaphore post to greater than one

c++,multithreading,semaphore
How can I make the value of Semaphore greater than one. I am trying to write a program that initiates two threads and have the two threads run concurrently after a certain condition is met. The program is trying to copy two arrays (b1 and b2) of size 5 to...

What happens when Binary Semaphore is Signalled twice? ie. s=1;wait(s); signal(s)signal(s); Does s becomes 0 or it remains 1?

operating-system,semaphore
What happens when Binary Semaphore is Signalled twice? i.e. Suppose s is binary semaphore variable with s=1 Now we do following-- wait(s),signal(s),signal(s) Does s becomes 0 or it remains 1?...

Java threads and POSIX threads, User or kernel level?

java,c,multithreading,operating-system,semaphore
Pretty new to threads and OS constructs here, was wondering if anyone could clear something up for me. When implementing threads in C you have access to POSIX threads, is a POSIX thread an OS level thread? Running at the kernel level? And in Java when creating and running threads,...

Objective-C: Wait to execute 'While' loop until NSURLConnection request is complete

ios,objective-c,nsurlconnection,semaphore,sendasynchronousrequest
Basically I want a way to issue a NSURLRequest multiple times in a loop until a certain condition has been met. I am using a rest api but the rest api only allows up to a maximum of 1,000 results at a time. So if i have, lets say 1,500...

Am i using semaphore wrong?

c#,multithreading,semaphore
I need to do some task in parallel using semaphore. I try this: Semaphore sema = new Semaphore(2,2); Thread[] Threads = new Thread[5]; for (int k = 0; k < 5; k++) { sema.WaitOne(); Console.WriteLine((k + 1) + " started"); Threads[k] = new Thread(ThreadMethod1); Threads[k].Start(k + 1); sema.Release(); } static...

condition synchronization with semaphores

c,semaphore
I have this example for which I'm trying to see what order the statements would run in. I believe it would be, X, A, B, Y. Would I be right in saying so. My logic is statement X runs first in parallel with A, but when we arrive at the...

Multithreaded semaphore program

c,multithreading,pointers,semaphore
I've spent quite a few hours on trying to figure this one out and I'm completly stuck. The program is supposed to start 6 threads. Where some threads start where others end. Right now, I'm trying to get one single thread (thread 0) to execute. The caps lock commenting shows...

Limiting simultaneously running asyncio coroutines with semaphores in a background thread

multithreading,semaphore,coroutine,event-loop,python-asyncio
As an experiment with Python's new asyncio module, I created the following snippet to process a set of long running actions (jobs) in a background worker. In an attempt to control the number of simultaneously running jobs, I introduced a semaphore in a with block (line 56). However, with the...

threading vs Synchronization

c++,multithreading,semaphore
I have a code in C++ that runs for more than 20000 times. the code solves multiple linear systems (matrices) and I am working on optimizing this code. I am just confused whether I should create a thread and kill it every iteration or just create the threads during the...

Semaphores secure shell C

c,process,semaphore,selectionchanging
Hey Guys what I have recently started and I am not very good at using semaphores. What I am trying to do is have one process display a number (e.g. 1) and the other process display 2 and then switch back to to the first process display 3 and then...

Process synchronization using semaphores

operating-system,synchronization,semaphore
I want to know if my approach in the following case is correct or not: There are 5 processes P1, P2, P3, P4 ad P5. And they can execute as follows: • P1 can start any time. • P2 can start after P1 completes. • P3 can start after P1...

Semaphore doesn't seem to work correctly with multiple threads

c,multithreading,semaphore
I try to write multiple threading example with C. Which is the gets number from user and sum numbers in two thread. Threads are directly away each other, front to back and back to front. I am using semaphore to manage global variables but output shows that semaphore do not...

Parse crash when calling [PFFacebookUtils initializeFacebook] - semaphore_wait_trap

ios,objective-c,parse.com,semaphore,pfuser
Since the latest Parse release (v1.6.3) my app gets stuck at launch, and the last breakpoint it hits is [PFFacebookUtils initializeFacebook]; If I hit pause and look at the debugger, the stack trace looks like this: I'm calling [PFFacebookUtils initializeFacebook] in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions as advised. From googling...

Using a semaphore [closed]

java,multithreading,semaphore
I would like to know how can i make those thread work one after the other, and how to make the first one always run after the second one. I created a semaphore, but i not sure about how to use it. public class A extends Thread { private int...

Visually where is the Deadlock? Why? How to avoid/fix?

operating-system,mutex,semaphore
I am trying to understand and solve the following problem. The following program attempts to use a pair of semaphores t and s for mutual exclusion. Initially: s = 1, t = 0. Thread 1 Thread 2 -------- -------- 1. P(s); P(s); 2. V(s); V(s); 3. P(t); P(t); 4. V(t);...

Access denied for a semaphore in a normal application, when it was first created by a service

c++,ms-access,semaphore
I create a global semaphore in a library that is used by a normal application and a service. If I run my normal application before the service, every thing comes fine, but if I change the order (I mean, running service before application), I will get access denied error in...

How can I create custom compiler warnings in java?

java,concurrency,semaphore,compiler-warnings
I'm looking for something that's similar to implementing the java.lang.AutoCloseable interface, where a compiler warning indicating Resource leak: 'xxxx' is never closed is generated. The use case for this is in a wrapper around a Synchronized Collection in java. The wrapper has an internal semaphore to prevent concurrent modification of...

How should I simulate sem_wait with a count?

c,posix,semaphore
I'm using semaphore.h and would like to acquire a semaphore if n instead of just one slot is available. Posix does not provide this natively. How can I work around that? I'm bound to using semaphores, no other means of synchronization are possible. I'm pondering using a binary semaphore with...

how to know python semaphore value

python,multithreading,semaphore
i use threading.semaphore in my code and i wonder if there is way i can use code like This if(sema.acquire()!=True): #do Somthing i want to use this piece of code in loop so i need to get if semaphore is taken or it's released or use code like this in...

Unnamed semaphore POSIX IPC

c,unix,posix,semaphore
I allocated a shared memory segment of the size of an integer. The expected outcome on stdout should be: P: 1 C: 2 But instead it is: C: 1 P: 2 Why is the child process not being blocked until parent is done and has unlocked the shared memory segment?...

creating threads using pthread.c

c,multithreading,pthreads,semaphore
I am trying to learn how to create threads in c using the pthread library, I am using the following code: #include <stdlib.h> #include <stdio.h> #include <semaphore.h> #include <pthread.h> static int glob = 0; static sem_t sem; static void *threadFunc(void *arg) { int loops = *((int *) arg); int loc,...

Consumer / producer does not wait for event

python,multithreading,semaphore,python-2.x
I want write a program producer/consumer, in this program I have a parent and a son, the parent fills a shared variable with a number of fish and it send notify at son. The son start eating, if there are not fish, it notify parent. I tried this code, but...

How can any thread signal for release of a binary semaphore

multithreading,concurrency,locking,mutex,semaphore
I am new to multithreading paradigm. While learning concurrency, every source I found says: "The difference between mutex and binary semaphore is the ownership i.e. a mutex can be signaled for release by only the thread who created it while a semaphore can be signaled any thread" Considering a scenario...

End background task and wait until app becomes active again - BLE processing data

ios,objective-c,cocoa-touch,semaphore
my app downloads a bunch of data through BLE from a peripheral. If I lock the screen my app gets moved into the background and it starts an background task. The download finishes fine but if the processing (which takes rather long because it is a lot of data) begins...

Can POSIX named semaphores synchronize threads?

multithreading,synchronization,posix,semaphore
I am looking for a way to synchronize several multi-threaded processes on Linux. C++11-style mutexes only operate among threads within a single process, ans as far as I know, flock doesn't mutually excludes threads of the same process. Therefore, I was thinking of POSIX semaphores. I know that unnamed semaphores...

Synchronization with semaphores

c,semaphore
This program operates on shared memory. It creates 4 child processes that increment the int value (initially 0) in the shared memory up to a MAXCOUNT (100k here). When a child sees that the int value in the shared memory is equal to MAXCOUNT, it stops incrementing it. Sometimes it...

What is the proper way to detect parent Task cancellation?

c#,c#-4.0,task,semaphore
I am developing a proof of concept app that factors a list of numbers using tasks and a semaphore, currently I have a List of Tasks,List<Task>, that take a FactorNumberClass and then calculate the factors of the specific number within the FactorNumberClass this currently works correctly. With each Task T,...

Possibility of Semaphoreslim.Wait(0) (to prevent multiple execution) causing non execution

c#,asynchronous,async-await,semaphore
The situation I am uncertain of concerns the usage of a "threadsafe" PipeStream where multiple threads can add messages to be written. If there is no queue of messages to be written, the current thread will begin writing to the reading party. If there is a queue, and the queue...