473,837 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1562
gi************* ****@noze.it (Gianguz) wrote in
news:af******** *************** ***@posting.goo gle.com:
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.12 4>...
gi************* ****@noze.it (Gianguz) wrote in
news:af******** *************** ***@posting.goo gle.com: 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(FakeS emaphore* 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<FakeS emaphore> guard(&_semapho re);
static T Value;

return Value;
}

static void _set(const T& t) {
FakeGuard<FakeS emaphore> guard(&_semapho re);
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
3040
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 good idea to create global objects like that? Maybe it won't work at all? code: class AbstractFactory { ...};
2
8836
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 { public: Book()
0
1205
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 sheet that defines some global constants as "xsl:variable" and some global templates, I then import this style sheet into another which uses these variables for various purposes.
10
1979
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 philosophy is from object orieted world but is it applicable for c?
15
2475
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 objects that the app used to speed up some global level data access and functionality. I have recoded the class libraries in .net and would like to acomplish the same functionality in asp.net.
4
7629
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
2268
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 confused things, so I will start afresh. An example of what I want to do...
53
26423
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 variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
5
1753
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 connection/communication, I usually define a class called DBBroker, When I used MFC, DBBrk wraped the Recordset/ODBC facilities and now it wraps the ADO.Net facilities:
0
9846
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9685
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10277
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5674
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.