Solved

#pragma data_seg("shared") and threads?

Posted on 1999-01-21
3
986 Views
Last Modified: 2013-11-20
In 16-bit land data in a dll could be shared. In 32-bit land the data can still be shared but must be declared in a #pragma data_seg compiler directive.

If I do this is there a new problem of the data being accessed across different thread boundaries? In which case do I have to place a critical section around access to all the shared data?
0
Comment
Question by:sdj_work
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
piano_boxer earned 50 total points
Comment Utility
As with any static variable(s) in your code, you have to protect it if it is accessed from multiple threads at the same time, and one or more threads of them are modifying the variable(s).

In your case you also have to use a named mutex (or simular) that can be accessed from all the processes the uses the DLL.

There is one problem with shared data segment in DLL. When another process loads the DLL and Windows loads it at another address because of a conflict with a another DLL, it will *not* get the same shared data as the other processes using your DLL. You can almost solve the problem by specifying the load address for the DLL, but your will not be 100% sure.

I suggest that you use memory mapped files ( without the file ) protected by a named mutex.

Here is how to initialize the mutex and shared memory mapped file:

...............

// Your own data structure goes here!
struct MY_DATA_STRUCT
{
    DWORD dwSomeValue;
    char  szSomeText[1024];
};

//
// Unique names of objects
//
LPCTSTR pszMutexName = "MyApp_Mutex";
LPCTSTR pszMapName = "MyApp_MemMap";

//
// Open the mutex and lock it.
//
BOOL bNew = TRUE;
HANDLE hMutex = CreateMutex(NULL, TRUE, pszMutexName);
if(GetLastError() == ERROR_ALREADY_EXISTS)
    bNew = FALSE;

//
// Create the memory mapped file for holding the
// MY_DATA_STRUCT structure.
//
HANDLE hFileMap = CreateFileMapping((HANDLE)0xffffffff, NULL,      PAGE_READWRITE | SEC_COMMIT, 0, sizeof(MY_DATA_STRUCT),pszMapName);

MY_DATA_STRUCT* pMyData = (MY_DATA_STRUCT*)MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);

//
// Zero out the memory if it did not exists all-ready.
//
if(bNew)
    ZeroMemory(pMyData, sizeof(MY_DAtA_STRUCT));

//
// Release lock on shared memory
//
ReleaseMutex(hMutex);

////////////////////////////////////////////
// ACCESSING DATA
////////////////////////////////////////////

// Gain exclusive access to the data
WaitForSingleObject(hMutex, INFINITE);

// Modify/read data frim the structure now.
pMyData->dwSomeValue += GetTickCount();

// Release the lock on the memory.
ReleaseMutex(hMutex);


////////////////////////////////////////////
// CLEANUP CODE:
////////////////////////////////////////////
UnmapViewOfFile(pMyData);
CloseHandle(hFileMap);
CloseHandle(hMutex);
0
 

Author Comment

by:sdj_work
Comment Utility
Is this point about the DLL process not getting the same shared area not covered by KB article: Q100634. In which case pointers would not actually work correctly but the data *is* still shared correctly?

Otherwise there would be no point in implementing the feature.
0
 
LVL 4

Expert Comment

by:piano_boxer
Comment Utility
This is from Q100634:

"if the block cannot be loaded into the same memory address, it is mapped to a different address, but it is still shared.".

So I guess I'm wrong about data not beeing shared when dll's are relocated, but I would still use the memmory mapped file method.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now