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 #52867 is a reply to message #52865] Thu, 19 December 2019 14:38 Go to previous messageGo to previous message
Tom1
Messages: 1303
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
 
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: Sat Jun 07 22:45:40 CEST 2025

Total time taken to generate the page: 0.04317 seconds