473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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

Gianguglielmo

Jul 22 '05 #1
1 2065
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
0
by: GianGuz | last post by:
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...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: Yuliaan | last post by:
Hi everyone! In program I wrote, I try to run 300 threads, when only 10 is running in the same time. I use array to create 300 threads, but first I start only 10, after that I add one thread when...
10
by: Ioannis Vranos | last post by:
In .NET multithreading we have to assign a thread to a method of a separate object each time (and not two methods of the same object)? In other words, why does this hung? #using...
7
by: Village | last post by:
I have installed PHP a number of times following tutorials from online and from a book. I have been able to process simple functions such at date() but when it comes to simply sending information...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.