By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,928 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,928 IT Pros & Developers. It's quick & easy.

A way to manage globals in multithreading environments?

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 13 Dec 2004 02:20:52 -0800, "gianguz" <gi*****************@noze.it>
wrote:
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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.