Process sharing variable # pragma data_seg usage

Source: Internet
Author: User
# Pragma data_seg Introduction

Use # pragma data_seg to create a new data segment and define shared data. The specific format is:

# Pragma data_seg ("shareddata ")

Hwnd sharedwnd = NULL; // share data

# Pragma data_seg ()
Bytes -----------------------------------------------------------------------------------------------------

1, # pragma data_seg () is generally used in DLL. That is to say, define a shared, named data segment in the DLL. The most important thing is that the global variables in this data segment can be shared by multiple processes. Otherwise, the global variables in the DLL cannot be shared among multiple processes.

2. Shared data must be initialized. Otherwise, the Microsoft compiler will put uninitialized data in the. BSS segment, leading to failure in sharing among multiple processes.

3. What you call correct results is an illusion. If you write this in a DLL:

# Pragma data_seg ("mydata ")

Int g_value; // note that the global is not initialized.

# Pragma data_seg ()

DLL provides two interface functions:

Int getvalue ()
{
Return g_value;
}

Void setvalue (int n)
{
G_value = N;
}

Then start two processes A and B, A and B, and call this DLL. If a calls setvalue (5 );
B then calls int M = getvalue ();
The m value is not necessarily 5, but an undefined value. Because the global data in DLL is private and cannot be shared for every process that calls it. If you initialize g_value, g_value will be put into the mydata segment. In other words, if a calls setvalue (5 );
B then calls int M = getvalue ();
Then the m value must be 5! This enables cross-process data communication!

Bytes ----------------------------------------------------------------------------------------------------
Sometimes we may want an applicationStart only onceLike Singleton, there may be a variety of implementation methods. Here we will talk about the methods implemented using # pragma data_seg, which is very simple and convenient.

Add

# Pragma data_seg ("flag_data ")
Int app_count = 0;
# Pragma data_seg ()
# Pragma comment (linker, "/section: flag_data, RWS ")

Add

If (app_count> 0) // exit the application if the count is greater than 0.
{
// MessageBox (null, "an application has been started", "warning", mb_ OK );
// Printf ("No % d Application", app_count );
Return false;
}
App_count ++;

Windows creates a wall around the address space of a Win32 program. Generally, the data in the address space of a program is private and invisible to other programs. However, multiple execution entities executing strprog indicate that it is no problem for strlib to share data among all execution entities of the program. When you add or delete a string in a strprog window, this change is immediately reflected in other windows.

Between all routines, strlib shares two variables: A character array and an integer (record the number of valid strings that have been stored ). Strlib stores these two variables in a shared special memory segment:

#pragma       data_seg ("shared")          int                  iTotal = 0 ;          WCHAR                szStrings [MAX_STRINGS][MAX_LENGTH + 1] = { '/0' } ;          #pragma       data_seg ()        

The first # pragma describes how to create a data segment. The name here is shared. You can name this section as any name you like. Here, # All initialized variables described by Pragma are stored in the shared data segment. The second # End Of The Pragma description section. Special initialization of variables is very important, otherwise the compiler will put them in common uninitialized data segments rather than in shared.

The connector must know that there is a shared data segment. In the Project Settings dialog box, select the link page. When "strlib" is selected, the "Project Options" field (both in the release and debug settings) contains the following link description:

/SECTION:shared,RWS        

The letter RWS indicates that the segment has read, write, and share attributes. Alternatively, you can use the DLL original code to specify the link options, just as we did in strlib. C:

#pragma comment(linker,"/SECTION:shared,RWS")        

The shared memory segment allows the itotal variable and the szstrings string array to be shared among all strlib routines. Because max_strings is 256 and max_length is 63, the length of the shared memory segment is 32,772 bytes-The itotal variable requires 4 bytes, and each of the 256 pointers requires 128 bytes.

 

 

Http://blog.csdn.net/lastsweetop/article/details/3835850

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.