473,385 Members | 1,676 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,385 software developers and data experts.

Global objects semantic with templates

I'd like to discuss about the opportunity to have a global objects
creator that introduces into a general framework
(suited for multithreading) a controlled semantic to manage
globals variables (objects and scalar-types).

In the following example Global is able to create objects of any kind
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:

#include <string>
#include <iostream>

using namespace std;

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); }

operator T&() { return __get(); }

~Global() { }

private:

static T& __get() {
//access can be serialized here for multithreading
static T Value;

return Value;
}

static void __set(const T& t) {
//access can be serialized here for multithreading
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<1, string> Global2;

Global2 c;

c = "I'm C";

cout << (string&)a << " " << (string&)b << endl;
}

Someone thinks that it can be useful? Or something like that is
already used. In that case can you give me some reference about.

Thanks,

Gianguglielmo
Jul 22 '05 #1
2 1526
gi*****************@noze.it (Gianguz) wrote in
news:af**************************@posting.google.c om:
I'd like to discuss about the opportunity to have a global objects
creator that introduces into a general framework
(suited for multithreading) a controlled semantic to manage
globals variables (objects and scalar-types).

In the following example Global is able to create objects of any kind
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:

#include <string>
#include <iostream>

using namespace std;

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); }

operator T&() { return __get(); }

~Global() { }

private:

static T& __get() {
//access can be serialized here for multithreading
static T Value;

return Value;
}

static void __set(const T& t) {
//access can be serialized here for multithreading
static T& Value = __get();

Value = t;

return;
}
};


Just some remarks:

1. Double underscore is reserved for compiler/stdlibrary implementations.
You may not use such identifiers in your own code.

2. *Any* globals in multithreading environment are quite suspect. In your
case the possible locking as indicated by the comments is probably at the
wrong place. There is often no use for locking separately the get() and
set() functions, as the set() function may thus silently overwrite the
results of set()-s called from other threads. One solution would be to
return a proxy object from the get() function, which locks the global
until set() function call on it, and/or scope exit. But this doesn't fit
well with your nice implicit conversion operators...

3. IIRC, statics in templates have been a troublesome area for some
compilers in the past, so this approach might practically not be as
portable as one might wish.

HTH
Paavo

Jul 22 '05 #2
Paavo Helde <pa***@ebi.ee> wrote in message news:<Xn**********************@194.126.101.124>...
gi*****************@noze.it (Gianguz) wrote in
news:af**************************@posting.google.c om: Just some remarks:

1. Double underscore is reserved for compiler/stdlibrary implementations.
You may not use such identifiers in your own code.

Right!;) Somewhat that can be used freely could be a single
underscore?
2. *Any* globals in multithreading environment are quite suspect. In your
case the possible locking as indicated by the comments is probably at the
wrong place. There is often no use for locking separately the get() and
set() functions, as the set() function may thus silently overwrite the
results of set()-s called from other threads. One solution would be to
return a proxy object from the get() function, which locks the global
until set() function call on it, and/or scope exit. But this doesn't fit
well with your nice implicit conversion operators...

I dont' clearly understand that.
If T1,T2,T3 call for instance 2 set and 1 get locking on the same
semaphore,
the get will simply take the last modified value.

For instance:

T1 T2 T3 RESULT
set('a') set('b') get() 'b'
set('b') set('a') get() 'a'
set('a') get() set('b') 'a'
set('b') get() set('a') 'b'
get() set('a') set('b') 'default'
get() set('b') set('a') 'default'

And that is the 'Faked' ;) code:

using namespace std;

class FakeSemaphore {

public:

FakeSemaphore() { }

void lock() { }

void unlock() { }

~FakeSemaphore() { }
};

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;
}

I agree about the proxy object as a general solution but in that case
I think locking should rely on the class itself to keep it
lightweight.
Do you think that this code could produce inconsistent
concurrent r/w sequences?
3. IIRC, statics in templates have been a troublesome area for some
compilers in the past, so this approach might practically not be as
portable as one might wish.

What kind of problems? What kind of mistake can the compiler (in my
case gcc works well) produce with a simple static variable declared
into a simple 2 typename class template?

Thanks!
HTH
Paavo

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oystein Haare | last post by:
I'm thinking about sort of a factory-system where the factories get instanciated on program start (through a global object in the .cpp file), and registers themselves with a singelton. Is it a...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
0
by: Kevin Fernandes | last post by:
(sorry if this is a re-post, I didn't see it show up on the list the first time) I have some questions about using global variables in included/imported style sheets with .NET. I have a style...
10
by: ankisharma | last post by:
Hi all At many places I have seen that programmers pass global variables to functions in c. I am not able to figure out why they do so. need some clues on this. somewhere i heard that this...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
4
by: John A Grandy | last post by:
I installed VS05 RC , created a new Web Site , but I do not see Global.asax , and I do not see Global.asax.cs in the App_Code dir ......
23
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
5
by: Saeed Amrollahi | last post by:
Dear all Hi I am Saeed Amrollahi. I write C++ programs using VC++ 2005 CLR/CLI. I have two problems: 1. How to declare/define and use global ref class objects? For example for database...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.