Solved

#pragma data_seg("shared") and threads?

Posted on 1999-01-21
3
1,002 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
ID: 1328099
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
ID: 1328100
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
ID: 1328101
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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