|
|
Home » U++ Library support » U++ MT-multithreading and servers » Use same variable in different threads
|
Re: Use same variable in different threads [message #30350 is a reply to message #30186] |
Sat, 25 December 2010 20:45 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
ScopedLock is in U++ as Mutex::Lock.
For read/write access, please notice RWMutex (multiple readers, just one writer). It has 'scoped' RWMutex::ReadLock and RWMutex::WriteLock variants.
As for Atomic variables, its basic feature is that they can accessed and perform increments/decrements from multiple threads WITHOUT caring about mutexes or barriers.
[Updated on: Sat, 25 December 2010 20:49] Report message to a moderator
|
|
|
|
Re: Use same variable in different threads [message #30454 is a reply to message #30453] |
Sun, 02 January 2011 03:08 |
gprentice
Messages: 260 Registered: November 2005 Location: New Zealand
|
Experienced Member |
|
|
It's time you learnt C++ templates Koldo
ScopedLock is a template class because it's declared like this
template<class MUTEX>
class ScopedLock
{
It has one template parameter named MUTEX. When you use a template class you have to specify template arguments corresponding to all the template parameters
i.e.
#include <Core/Mt.h>
struct mySharedData {
int a, b;
String c;
Mutex m;
};
mySharedData data;
// ...
ScopedLock<Mutex> xyz(data.m);
where Mutex (the template argument in between <>) is the U++ mutex class in core/mt.h
You also need to change the call mutex.lock() to mutex.Enter() and mutex.unLock() to mutex.Leave
It's actually pretty much pointless to make the ScopedLock class a template class so you could either change it to a normal class or maybe put a default argument
template<class MUTEX = Mutex>
class ScopedLock
{
then you can do
// ...
ScopedLock xyz(data.m);
When xyz goes out of scope, the destructor is called which releases the mutex.
By the way, this code is invalid as you can't call a constructor.
ScopedLock(data.m);// Enter the section that ...
The Mutex::Lock class that Mirek mentioned, creates a temporary mutex so can't be used in your case if you have other code elsewhere that is accessing the data.
Graeme
[Updated on: Sun, 02 January 2011 03:09] Report message to a moderator
|
|
|
|
|
Re: Use same variable in different threads [message #30459 is a reply to message #30350] |
Sun, 02 January 2011 15:35 |
|
mirek wrote on Sat, 25 December 2010 21:45 | ScopedLock is in U++ as Mutex::Lock.
For read/write access, please notice RWMutex (multiple readers, just one writer). It has 'scoped' RWMutex::ReadLock and RWMutex::WriteLock variants.
As for Atomic variables, its basic feature is that they can accessed and perform increments/decrements from multiple threads WITHOUT caring about mutexes or barriers.
|
Mirek,
You read my mind. RWMutex seems to be perfect.
Windows variant I see
1. a limitation: only LONG_MAX concurrent reads can be. if concurrent reads > LONG_MAX then result is undefined.
Linux variant is a kernel variant. I didn't found the source code and can't say the opinion.
2. If write in recursive mode by the same phread will wait to infinitely.
The linux version is more pretty realized according by this links source code:
http://www.jbox.dk/sanos/source/include/pthread.h.html
http://www.jbox.dk/sanos/source/lib/pthread/rwlock.c.html
|
|
|
|
|
|
|
Goto Forum:
Current Time: Thu Apr 25 01:49:25 CEST 2024
Total time taken to generate the page: 0.03121 seconds
|
|
|