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 » U++ Library support » U++ MT-multithreading and servers » Simple class to handle variables used by different threads
Simple class to handle variables used by different threads [message #45026] Fri, 21 August 2015 13:41 Go to previous message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello all

I wanted to ask you if this class could serve to manage variables that may be read and changed from different threads
template <class T>
class threadSafe {
public:
	threadSafe() 		{}
	threadSafe(T v) 	{val = v;}
	void operator=(T v)	{BarrierWrite(val, v);}
	operator T() 		{return ReadWithBarrier(val);}
   
private:
	volatile T val;
};

It can be used as simple as this:
threadSafe<int> val = 23;
double d = val + 3.5;


Other sample in two threads:
// Main thread
threadSafe<bool> thread1Busy;

// Thread 1
thread1Busy = true;
...
thread1Busy = false;

// Thread 2
...
while (thread1Busy)
	Sleep(100);
...


Best regards
IƱaki
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to just Post data ?
Next Topic: Mt.cpp broken compile using 9604 nightly snap-shot
Goto Forum:
  


Current Time: Fri Mar 29 10:43:41 CET 2024

Total time taken to generate the page: 0.01198 seconds