Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 #52868 is a reply to message #52867] Thu, 19 December 2019 17:05 Go to previous messageGo to previous message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
Tom1 wrote on Thu, 19 December 2019 14:38
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


Sounds good.

Unfortunately, I have removed -1 logic from your code, so that will make it more complicate. I guess I will put it back after all.
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Linux binary application distribution options?
Next Topic: Adding Version Information & Icons to app's txt file
Goto Forum:
  


Current Time: Sun Jun 08 20:38:24 CEST 2025

Total time taken to generate the page: 0.04463 seconds