Home » Community » Coffee corner » What is the best way to create a Semaphore with timeout?  
	
		
		
			| Re: What is the best way to create a Semaphore with timeout? [message #52867 is a reply to message #52865] | 
			Thu, 19 December 2019 14:38    | 
		 
		
			
				
				
				
					
						
						Tom1
						 Messages: 1305 Registered: March 2007 
						
					 | 
					Ultimate Contributor  | 
					 | 
		 
		 
	 | 
 
	
		Hi! 
 
That was fast! Thanks Mirek!! 
 
Now to the Fifo implementation. Does this look right from the point of mutex synchronization? I know this 'appears to work when testing' but I'm new with ConditionVariable, so I'm a bit uncertain if my usage of it is correct. I.e. Will multiple receiving threads calling StringFifo::Get() be correctly served so that each and every String will get read exactly once? 
 
class StringFifo: public BiVector<String>{
	ConditionVariable cv;
	Mutex mtx;
	
public:
	StringFifo(){
		
	}
	
	void Put(const String &s){
		mtx.Enter();
		AddTail(s);
		cv.Signal();
		mtx.Leave();
	}
	
	String Get(int timeout_ms=-1){
		mtx.Enter();
		if(GetCount() || (cv.Wait(mtx,timeout_ms) && GetCount())){
			String r=PopHead();
			mtx.Leave();
			return r;
		}
		else{
			mtx.Leave();
			return String::GetVoid();
		}
	}
};
 
 
Thanks and best regards, 
 
Tom
		
		
		
 |  
	| 
		
	 | 
 
 
 |  
  
 
	
	  | 
	 | 
	
		What is the best way to create a Semaphore with timeout?
		By:  Tom1 on Tue, 17 December 2019 09:49  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  Tom1 on Tue, 17 December 2019 10:49  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  Tom1 on Tue, 17 December 2019 11:53  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  mirek on Thu, 19 December 2019 11:38  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  Tom1 on Thu, 19 December 2019 12:59  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  mirek on Thu, 19 December 2019 13:40  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  Tom1 on Thu, 19 December 2019 14:38  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  mirek on Thu, 19 December 2019 17:05  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  Tom1 on Thu, 19 December 2019 21:44  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  mirek on Wed, 22 January 2020 14:44  
	 | 
 
	  | 
	 | 
	
		Re: What is the best way to create a Semaphore with timeout?
		By:  Tom1 on Wed, 22 January 2020 14:48  
	 | 
  
Goto Forum:
 
 Current Time: Tue Nov 04 09:10:37 CET 2025 
 Total time taken to generate the page: 0.05089 seconds 
 |