CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2017
    Posts
    12

    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.

  2. #2
    Join Date
    May 2017
    Posts
    12

    Re: How to share CCriticalSection between modules

    It was a typo, it should have been %d and that does read the correct value.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  4. #4
    Join Date
    May 2017
    Posts
    12

    Re: How to share CCriticalSection between modules

    Quote Originally Posted by 2kaud View Post
    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?

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to share CCriticalSection between modules

    Quote Originally Posted by caezar View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured