A way to manage globals in multithreading environments?



 DEVELOP > c-Plus-Plus > A way to manage globals in multithreading environments?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1
Topic: DEVELOP > c-Plus-Plus
User: "GianGuz"
Date: 14 Dec 2004 05:53:54 AM
Object: A way to manage globals in multithreading environments?
In the following example Global is able to create and manage access
to objects of any kind (even in a multithreading environment) with
and index value attached to. So a Global<0, string> is a unique
string instance object allocated into the system that can be accessed
from any point into the program simply 'creating' another instance of
Global<int, T> with the same id and type.Like globals, destruction
of these objects is delegated until the lifetime expiration of the
program.
Here is the example:
static FakeSemaphore _semaphore;
template<class LOCK> class FakeGuard {
private:
FakeSemaphore* sem;
FakeGuard();
public:
FakeGuard(FakeSemaphore* s) { sem = s; sem->lock(); }
~FakeGuard() { sem->unlock(); }
};
template<int I,class T> class Global {
public:
static const int Index = I;
typedef T Type;
Global() { }
Global(const T& t) { _set(t); }
const Global& operator =(const T& t) { _set(t); return *this; }
operator T&() { return _get(); }
~Global() { }
private:
static T& _get() {
FakeGuard<FakeSemaphore> guard(&_semaphore);
static T Value;
return Value;
}
static void _set(const T& t) {
FakeGuard<FakeSemaphore> guard(&_semaphore);
static T& Value = _get();
Value = t;
return;
}
};
int main() {
typedef Global<0, string> Global0;
typedef Global<1, string> Global1;
Global0 a;
Global1 b;
a = "I'm A";
b = "I'm B";
cout << (string&)a << " " << (string&)b << endl;
a = b;
cout << (string&)a << " " << (string&)b << endl;
typedef Global<b.Index, Global1::Type> Global2;
Global2 c;
c = "I'm C";
cout << (string&)a << " " << (string&)b << endl;
}
Question is:
it can be useful? using globals instances of any kind in multithreading
is always an
untouchable argument, but what about to rediscuss this?
Thanks,
Gianguglielmo
.

 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER