|
|
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 |
|
koldo
Messages: 3394 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
|
|
|
|
|
|
|
|
|
|
Re: Simple class to handle variables used by different threads [message #45049 is a reply to message #45046] |
Mon, 24 August 2015 19:13 |
|
mirek
Messages: 14039 Registered: November 2005
|
Ultimate Member |
|
|
Well, depends on type and CPU...
Usually, if type is directly supported by CPU, it works.
Example of type that does not work is e.g. int64 in 32-bit mode. It is because it is, on CPU level, compound type...
Anyway, MT is not hard because of transfering data between threads. If you think about it, they have to get transfered at some point... The real issues are order:
if one thread does
a = a1;
b = b1;
then another thread can see b updated first. This is what barriers are for.
(Then, of course, there is an issue that you cannot never tell WHEN the change is visible in another thread, but that is really not a problem...)
And then, of course, serialization, which is about transactions:
a = a + 1;
now this, on CPU level is something like
read a into register
increment register // remember this point A
write register into a
Now imagine if another thread starts incrementing a at point A...
Well, there really is complex theory about lock-less multithreading, it is pretty tough stuff. I would not dare suggesting new MT tools before reading it all
Mirek
|
|
|
Re: Simple class to handle variables used by different threads [message #45051 is a reply to message #45049] |
Tue, 25 August 2015 00:13 |
|
koldo
Messages: 3394 Registered: August 2008
|
Senior Veteran |
|
|
Thank you Mindtraveller and Mirek
In summary, could a general all purpose thread safe class be like this?
template <class T>
class ThreadSafe {
public:
ThreadSafe() {}
ThreadSafe(T v) {val = v;}
void operator=(T v) {
mutex.EnterWrite();
val = v;
mutex.LeaveWrite();
}
operator T() {
T tmp;
mutex.EnterRead();
tmp = val;
mutex.LeaveRead();
return tmp;
}
operator++()....
private:
volatile T val;
RWMutex mutex;
};
Best regards
Iñaki
[Updated on: Tue, 25 August 2015 00:13] Report message to a moderator
|
|
|
Re: Simple class to handle variables used by different threads [message #45053 is a reply to message #45051] |
Tue, 25 August 2015 00:28 |
|
It's better if you decide with mutex, volatile, atomic and barriers and in every custom case.
If you build multi-thread program, then you must achieve some goals, like performance or other.
But situation is change very fast. Still come in Intel CoreI7, single thread programs is faster or same as multi-thread (in high-load segment), because is needed some time for switching processor context.
SergeyNikitin<U++>( linux, wine )
{
under( Ubuntu || Debian || Raspbian );
}
|
|
|
|
|
|
|
Re: Simple class to handle variables used by different threads [message #45060 is a reply to message #45059] |
Wed, 26 August 2015 05:01 |
|
For me:
1) I decide, that common variables of all threads, I place to ThreadStore (analog of your ThreadSafe).
2) Most of variables not needed to place to ThreadStore class and it's locals for thread.
3) I plan and program custom processing for every member-variable of ThreadStore class.
Note (1), that most of Container type (Vectors, Arrays, Indexes) are not thread-safe!!!
Note (2), that Write/Read barrier is needed same way as mutexes/semafors for synchronisation data access. (Mutex/semafors - prevent inter-thread collision, Write/Read barriers - prevent inter-processor-cores collisions)
Your class uses only Write/Read barriers.
If you place under mutex of all ThreadSafe class and all it's member, then performance is become loss. Custom variable processing more fast and safe way.
----
PS
If I'm mistaken - pls correct.
SergeyNikitin<U++>( linux, wine )
{
under( Ubuntu || Debian || Raspbian );
}
[Updated on: Wed, 26 August 2015 05:42] Report message to a moderator
|
|
|
|
Re: Simple class to handle variables used by different threads [message #45066 is a reply to message #45061] |
Thu, 27 August 2015 01:37 |
|
ThreadStore as Global or Module vars place for thread . Individually for every project. Very useful.
I can't place full example (not mine, proprietary projects).
But it's nothing difficult! Mutexes, Write Read Barriers by atomic. Every Thread interchange vars - volatile.
MSDN Magazine Example - in CHM file (last link) has C \ C++ discussion.
SergeyNikitin<U++>( linux, wine )
{
under( Ubuntu || Debian || Raspbian );
}
|
|
|
Goto Forum:
Current Time: Sat Sep 21 03:01:16 CEST 2024
Total time taken to generate the page: 0.07225 seconds
|
|
|