A way to manage globals in multithreading environments?

G

gianguz

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 mutlithreading
is always an untouchable argument, but what about to rediscuss this?
Thanks,

Gianguglielmo
 
T

Tom Widmer

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.
Question is:
it can be useful?

Yes, but also dangerous, and I think most people use thread safe
singletons instead.
using globals instances of any kind in mutlithreading
is always an untouchable argument, but what about to rediscuss this?

Well, using globals in any environment is generally an untouchable
argument! And singletons are bad enough...
http://c2.com/cgi/wiki?GlobalVariablesAreBad
http://c2.com/cgi/wiki?SingletonsAreEvil
http://c2.com/cgi/wiki?SingletonGlobalProblems

I find myself over time refactoring code to remove any globals and
singletons. The problem with singletons and globals is that they can
be accessed and modified by any code in your entire project, which,
when you have 2000+ source files, becomes a bit of a headache. If you
explicitly pass around pointers/references instead, you control which
code gets to use a particular object in your design, and it becomes
simpler if you want to start using different objects in different
places.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top