472,784 Members | 996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 1501
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.