Home » U++ Library support » U++ MT-multithreading and servers » Use same variable in different threads
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
|
|
|
|
|
Use same variable in different threads
By: koldo on Thu, 09 December 2010 18:26
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: koldo on Thu, 09 December 2010 22:04
|
|
|
Re: Use same variable in different threads
By: Didier on Fri, 10 December 2010 00:26
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: koldo on Fri, 10 December 2010 10:01
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: koldo on Fri, 10 December 2010 13:19
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: Didier on Fri, 10 December 2010 18:50
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: Didier on Sat, 11 December 2010 23:13
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: mirek on Sun, 12 December 2010 09:43
|
|
|
Re: Use same variable in different threads
By: koldo on Sun, 12 December 2010 23:16
|
|
|
Re: Use same variable in different threads
By: tojocky on Mon, 13 December 2010 11:35
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: Didier on Mon, 13 December 2010 22:48
|
|
|
Re: Use same variable in different threads
By: koldo on Tue, 14 December 2010 09:02
|
|
|
Re: Use same variable in different threads
By: mirek on Sat, 25 December 2010 20:45
|
|
|
Re: Use same variable in different threads
By: tojocky on Sun, 02 January 2011 15:35
|
|
|
Re: Use same variable in different threads
By: mirek on Sun, 02 January 2011 18:38
|
|
|
Re: Use same variable in different threads
By: koldo on Sun, 02 January 2011 19:06
|
|
|
Re: Use same variable in different threads
By: mirek on Sun, 02 January 2011 22:56
|
|
|
Re: Use same variable in different threads
By: koldo on Mon, 03 January 2011 09:17
|
|
|
Re: Use same variable in different threads
By: koldo on Sun, 02 January 2011 01:12
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: Didier on Sun, 02 January 2011 12:18
|
|
|
Re: Use same variable in different threads
|
Goto Forum:
Current Time: Thu May 09 14:09:02 CEST 2024
Total time taken to generate the page: 0.02758 seconds
|