U++ framework
Do not panic. Ask here before giving up.

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: 3458
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: Sun Apr 26 23:01:44 GMT+2 2026

Total time taken to generate the page: 0.00822 seconds