Declare Shared Variable in VC++

Shared Variable – Shared variables are those variables which can be shared in multiple instance of the same application. Suppose we are running three instance of an exe, then the value of shared variable will be same in all three instances. If any instance change the value of shared variable then it will be reflect in all instance.

In short shared variable is same as static variable, the only difference is that static variable contain same value for all instance of class and shared variable contains same value for all instance of application.

To declare shared variable in VC++, following code can be use in .cpp file

// shared data
#pragma data_seg(“.SHARED”)
int IsHomePageSet = -1;
#pragma data_seg()
#pragma comment(linker, “/section:.SHARED,RWS”)
// end shared data

In above code, IsHomePageSet is declared as a shared variable. This variable will be use as normal variable. Value of this variable will be same in all the instance of application.

Advertisements
Posted in Uncategorized. Leave a Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: