How to share CCriticalSection between modules
I am in a situation where I need to synchronize two threads in two different dll modules synchronize around the same CCriticalSection object. I can define the CCriticalSection in one module but how can ther module access it?
I would need to place the following call in corresonding function in each thread.
Code:
CSingleLock lock( &theCriticalSection, TRUE);
I tried using this approach but no luck. I tested with a simple int variable but it doesn't recognize it in the other dll (although it buids).
Code:
#pragma data_seg("shared")
__declspec(dllexport) CCriticalSection theCriticalSection;
__declspec(dllexport) int number = 7;
#pragma data_seg()
#pragma comment(linker, "/section:shared,RWS")
I declare this as following in the dll where I want to use it:
__declspec(dllimport) CCriticalSection theCriticalSection;
__declspec(dllimport) int number;
Later in the function TRACE("number = %s", number ); asserts because it can't apparently link/recognize to `number`variable.
Re: How to share CCriticalSection between modules
It was a typo, it should have been %d and that does read the correct value.
Re: How to share CCriticalSection between modules
Variables defined in the data_seg must be initialised where they are defined. So in your example number is OK but theCriticalSection isn't.
Re: How to share CCriticalSection between modules
Quote:
Originally Posted by
2kaud
Variables defined in the data_seg must be initialised where they are defined. So in your example number is OK but theCriticalSection isn't.
I thought about that too but doesn't a class initialize itself in a constructor? When I tried to initialize it, it was to be what the constructor requires.
Also I don't know what really is the use of data_seg section because I end up sharing the variable without it and it works and I can access it in the other dll, so do we really need the data_seg?
Re: How to share CCriticalSection between modules
Putting it in the data_seg AFAIK doesn't call any initialiser/constructor by default. The variable has to be manually initialised. I've never tried MFC classes in a data_seg. The use of a data_seg is to share the variables between programs. From MSDN
Quote:
your DLL needs to share data with other Win32 DLLs loaded by a different application
Re: How to share CCriticalSection between modules
Critical section is for synchronizing threads that belong to the same process. If you want to synchronize threads from different processes (applications), use a (named) mutex.
See Mutex Objects in MSDN.
Re: How to share CCriticalSection between modules
Quote:
Originally Posted by
caezar
I am in a situation where I need to synchronize two threads in two different dll modules synchronize around the same CCriticalSection object. I can define the CCriticalSection in one module but how can ther module access it?
I would recommend not to access the critical section object directly, but expose from the hosting dll a couple of functions, something like MyCriticalSectionEnter() and MyCriticalSectionLeave(), where you actually call the corresponding object methods.