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 next message
koldo is currently offline  koldo
Messages: 3268
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 #45032 is a reply to message #45026] Sat, 22 August 2015 14:00 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 881
Registered: September 2012
Location: Poland, Kraków
Experienced Contributor
Hello,

IMHO, You should name your class ThreadSafe instead of threadSafe (According to the Ultimate++ coding standard).

Sincerely,
Klugier


Ultimate++ - one framework to rule them all.
Re: Simple class to handle variables used by different threads [message #45034 is a reply to message #45032] Sat, 22 August 2015 16:08 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
Smile

And do you think is right? It is so simple that I am imagining that something is wrong, either in the implementation or in the concept.


Best regards
Iñaki
Re: Simple class to handle variables used by different threads [message #45040 is a reply to message #45026] Sun, 23 August 2015 20:34 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13428
Registered: November 2005
Ultimate Member
koldo wrote on Fri, 21 August 2015 13:41
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);
...


I would be quite worried about

threadSafe<int> x;

x = x + 1;


Also, I am not quite sure that barriers are used correctly here.. What are they supposed to do?

BTW, second example would work fine without any synchronization at all...

Mirek
Re: Simple class to handle variables used by different threads [message #45043 is a reply to message #45040] Mon, 24 August 2015 09:05 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
Hello Mirek

The goal is to have variables that can be read and written safely in different threads. Of course there will be an efficiency penalty Smile .


Best regards
Iñaki
Re: Simple class to handle variables used by different threads [message #45044 is a reply to message #45043] Mon, 24 August 2015 11:34 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13428
Registered: November 2005
Ultimate Member
koldo wrote on Mon, 24 August 2015 09:05
Hello Mirek

The goal is to have variables that can be read and written safely in different threads. Of course there will be an efficiency penalty Smile .


Well, but "can be read and written" is very broad term...

E.g. most primitive types can already be written/read "safely" (without synchronization). The issue with MT is not this, but "transactions", where several reads and writes are grouped and should form a transaction. Like x = x + 1.
Re: Simple class to handle variables used by different threads [message #45046 is a reply to message #45044] Mon, 24 August 2015 15:05 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
Does this mean that declaring a variable as volatile is enough to read and write it from different threads safely?

Best regards
Iñaki
Re: Simple class to handle variables used by different threads [message #45047 is a reply to message #45046] Mon, 24 August 2015 19:08 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Of course, making variable 'volatile' actually doesn't guarantee anything when we discuss multithreading issues. It just says to compiler 'please, don't optimize it as it may be changed outside your source code'.

The reason it 'sort of' works under modern x86/x64 systems is that modern CPUs change char/int variable with single CPU instruction which effectively avoids multithreading issues. If your variable is anything but char/int (+int64 under x64) you may have multithreading issues when variable is accessed while being updated in another thread.

You should also notice that under arm/mips systems you may have issues even with int32/int64 depending on particular CPU used.

I guess we have nice solution in U++. There's Atomic type which guarantees no multithreading issues with possible support with native OS/CPU commands. So why should we really duplicate its functionality?
Re: Simple class to handle variables used by different threads [message #45049 is a reply to message #45046] Mon, 24 August 2015 19:13 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13428
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 Smile

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 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
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 Go to previous messageGo to next message
sergeynikitin is currently offline  sergeynikitin
Messages: 746
Registered: January 2008
Location: Moscow, Russia
Contributor

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 #45055 is a reply to message #45051] Tue, 25 August 2015 15:44 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13428
Registered: November 2005
Ultimate Member
koldo wrote on Tue, 25 August 2015 00:13
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;
};


The problem is this does not solve many issues.... (like, almost nothing Smile

Mirek
Re: Simple class to handle variables used by different threads [message #45056 is a reply to message #45055] Tue, 25 August 2015 17:04 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
Thank you all for your posts. You all are experts in MT and underline the things I am doing wrong.

Could you give some simple examples of how to do the things right using U++?. For example a slow process gathering data of different non int variables (String, ...) and another process that will get that data continuously, but when available.



Best regards
Iñaki
Re: Simple class to handle variables used by different threads [message #45057 is a reply to message #45056] Tue, 25 August 2015 19:27 Go to previous messageGo to next message
sergeynikitin is currently offline  sergeynikitin
Messages: 746
Registered: January 2008
Location: Moscow, Russia
Contributor

Very useful short article
http://blog.csdn.net/win_lin/article/details/8274810
(msdn article, but I found it only on china)

Russian link: http://www.cyberguru.ru/microsoft-net/csharp-net/multithread -code.html?showall=1

Article found in October'2008 MSDN Magazine (https://msdn.microsoft.com/en-us/magazine/ee310108.aspx:
http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-4 1f7-9e6d-3aa95b5a6aea/MSDNMagazine2008_10en-us.chm)

(entity actual in .Net and C++)


SergeyNikitin<U++>( linux, wine )
{
    under( Ubuntu || Debian || Raspbian );
}

[Updated on: Tue, 25 August 2015 20:03]

Report message to a moderator

Re: Simple class to handle variables used by different threads [message #45059 is a reply to message #45057] Tue, 25 August 2015 22:45 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
sergeynikitin wrote on Tue, 25 August 2015 19:27
Very useful short article
http://blog.csdn.net/win_lin/article/details/8274810
(msdn article, but I found it only on china)

Russian link: http://www.cyberguru.ru/microsoft-net/csharp-net/multithread -code.html?showall=1

Article found in October'2008 MSDN Magazine (https://msdn.microsoft.com/en-us/magazine/ee310108.aspx:
http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-4 1f7-9e6d-3aa95b5a6aea/MSDNMagazine2008_10en-us.chm)

(entity actual in .Net and C++)
Thank you Sergey, but I can only see articles in Visual Basic and C#.

I have read many articles explaining many different ways to do many things wrong (with many contradictions between authors...), but almost nothing about how to do a few things right, including U++ Crying or Very Sad


Best regards
Iñaki
Re: Simple class to handle variables used by different threads [message #45060 is a reply to message #45059] Wed, 26 August 2015 05:01 Go to previous messageGo to next message
sergeynikitin is currently offline  sergeynikitin
Messages: 746
Registered: January 2008
Location: Moscow, Russia
Contributor

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 #45061 is a reply to message #45060] Wed, 26 August 2015 07:24 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
Thank you Sergey Smile

I have not found references to "ThreadStore" so I imagine it is a class developed by you.
Custom variable processing more fast and safe
Could you put an example of this with U++?


Best regards
Iñaki
Re: Simple class to handle variables used by different threads [message #45066 is a reply to message #45061] Thu, 27 August 2015 01:37 Go to previous message
sergeynikitin is currently offline  sergeynikitin
Messages: 746
Registered: January 2008
Location: Moscow, Russia
Contributor

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 );
}
Previous Topic: How to just Post data ?
Next Topic: Mt.cpp broken compile using 9604 nightly snap-shot
Goto Forum:
  


Current Time: Sat Sep 25 08:45:09 CEST 2021

Total time taken to generate the page: 0.01829 seconds