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 » 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 Go to previous messageGo to previous message
gprentice is currently offline  gprentice
Messages: 260
Registered: November 2005
Location: New Zealand
Experienced Member
It's time you learnt C++ templates Koldo Smile

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

 
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
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: OpenMP
Next Topic: Different native pthread.h implementations
Goto Forum:
  


Current Time: Thu May 09 14:09:02 CEST 2024

Total time taken to generate the page: 0.02758 seconds