semaphore

sem_timedwait() pthreads-win32 errno usage

廉价感情. 提交于 2019-12-24 09:24:23
问题 I am using pthreads-win32 for threads on a Win32 Application. I am calculating the timespec using the first function posted here. The call to sem_timedwait() appears to be waiting for the specified ts time, however every time it completes I get the following message: Error waiting on semaphore: No error I have checked the file of sem_timedwait.c here, and they specify the same errno values and return values. Consequently, I do not know why it is exiting with this errno, and would like to know

sem_wait not working in basic code

风格不统一 提交于 2019-12-24 04:13:30
问题 Compiled with gcc. I ran this to see why the semaphores I was using in my other programs were not working correctly. Am I just using these them incorrectly or what? The string is outputted every time even though the semaphore should halt execution and cause a deadlock, right? Here is the code: #include <pthread.h> #include <semaphore.h> #include <stdlib.h> #define NUM_THREADS 5 void printHello(); int main(){ int i; pthread_t threads[NUM_THREADS]; sem_t sem1; sem_init(&sem1, 0, 0); sem_wait(

Producer Consumer using semaphores and mutexes in Python

社会主义新天地 提交于 2019-12-24 03:52:40
问题 I'm trying to understand how to implement a Queue with a bounded buffer size that can be used by multiple producers and consumers using Python Semaphores. Here's my implementation: class Q: def __init__(self, size): self.buff = [None]*size self.end = 0 self.start = 0 self.size = size self.end_lock = Lock() # protect end from race across multiple producers self.start_lock = Lock() # protect start from race across multiple consumers self.open = Semaphore(size) # block till there's space to

Implementation of monitors with semaphores

ぐ巨炮叔叔 提交于 2019-12-24 00:47:23
问题 I've been asked the following question and I'm not sure what the correct answer is to it: If monitors are implemented by replacing condition variables with semaphores (counters set to 0) with down() and up() as wait and signal, respectively, would the monitors work correctly? I'd be tempted to say it is a correct implementation because semaphores and condition variables can replace each other, correct? Is there a better explanation? 回答1: You are asking about a semaphore initialized to 1,

Java Multithreading Semaphore

可紊 提交于 2019-12-24 00:03:25
问题 If I launch for example five threads in main class that will be running parralell final int TERMINAL_COUNT = 5; Semaphore semaphore = new Semaphore(1); Queue<Terminal> terminals = new LinkedList<>(); for (int i = 1; i<= TERMINAL_COUNT; i++){ terminals.offer(new Terminal(i, semaphore)); } for(Terminal terminal : terminals) terminal.start(); } And class that carries them looks like public class Terminal extends Thread { private Dispetcher dispetcher; private Semaphore semaphore; public Terminal

Can C++11 condition_variables be used to synchronize processes?

南笙酒味 提交于 2019-12-23 20:21:25
问题 I'm trying to learn about C++11's std::condition_variable . I've read the articles at cppreference.com and cplusplus.com as well as C++0x has no semaphores? How to synchronize threads?. My question, which I think has not been answered by the three noted articles, is: can "semaphores" that are created with a combination of of std::mutex and std::condition_variable (see the answers to C++0x has no semaphores? How to synchronize threads?) be used to synchronize between processes the way posix

Java unlimited semaphore

旧巷老猫 提交于 2019-12-23 18:27:22
问题 Wondering how to not limit connections (or anything) using a Semaphore. So you might be thinking, "That sounds dumb." But, it simplifies my code a bit as it lets me treat the limited and unlimited cases uniformly. Note I'm not looking for an advice on how to write something like if(limited) { semaphore.acquire(); } I can come up with dozens of ways to do this forking with if-statements. More specifically I'm looking for an Apache Commons or Java solution. This is just a simple situation in

single-lane bridge problem

笑着哭i 提交于 2019-12-23 18:09:06
问题 If you're unfamiliar with the problem, it's something like this. I didn't come to ask for an answer, I have actually finished all of my coding. I have just found that my solution doesn't solve it the best way possible, because my solution only allows one car at a time on the bridge. I was hoping I could get some tips about how to go about using sem_wait and sem_post to solving this problem. I hope to allow traffic flowing the same direction to flow together and not one at a time. My solution

Is it possible to avoid a wakeup-waiting race using only POSIX semaphores? Is it benign?

雨燕双飞 提交于 2019-12-23 05:58:11
问题 I'd like to use POSIX semaphores to manage atomic get and put from a file representing a queue. I want the flexibility of having something named in the filesystem, so that completely unrelated processes can share a queue. I think this plan rules out pthreads. The named posix semaphores are great for putting something in the filesystem that any process can see, but I can't find the standard CondWait primitive: ... decide we have to wait .... CondWait(sem, cond); When CondWait is called by a

Semaphores to run asynchronous method synchronously

夙愿已清 提交于 2019-12-23 05:05:13
问题 In my app, I use an asynchronous class method, that I need to run synchronously. As far as I understand, I should use semaphores to accomplish this. Using semaphores is something I never had to do before, so now I'm struggling to put them in the right place. I hope some of you could help me out here. This is my code: -(void)CreateNewerEventInCalendar:(Event*)myEvent{ [MyCalendar requestAccess:^(BOOL granted, NSError *error) { if (granted) { BOOL result = [MyCalendar addEventAt:myEvent