-
May 1st, 2017, 01:46 PM
#1
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.
-
May 1st, 2017, 01:53 PM
#2
Re: How to share CCriticalSection between modules
It was a typo, it should have been %d and that does read the correct value.
-
May 1st, 2017, 04:51 PM
#3
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 2nd, 2017, 10:40 AM
#4
Re: How to share CCriticalSection between modules
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?
-
May 2nd, 2017, 10:53 AM
#5
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
your DLL needs to share data with other Win32 DLLs loaded by a different application
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 3rd, 2017, 09:07 AM
#6
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.
-
May 6th, 2017, 05:36 PM
#7
Re: How to share CCriticalSection between modules
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.
Best regards,
Igor
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|