none
Lock and Unlock functions for DOT.NET RRS feed

  • Question

  • Hello everybody,

    I am looking for multi-threaded managed functions (Lock and Unlock) that can work managed on DOT.NET like InitializeCriticalSection and LeaveCriticalSection in native windows or any other cross functions.

    thanks in advance

    Friday, July 19, 2019 6:38 AM

Answers

All replies

  • Hello everybody,

    I am looking for multi-threaded managed functions (Lock and Unlock) that can work managed on DOT.NET like InitializeCriticalSection and LeaveCriticalSection in native windows or any other cross functions.

    thanks in advance

    I want to lock and unlock unmanaged code inside managed code. I already have lock and unlock functions I want to reimplement in managed code. This is because my code is cross between platforms


    _CLASS __oLockBaseEmp// : public __oRef
    {
    LOCKOBJECTTYPE m_lock;
    public:
    __oLockBaseEmp();
    virtual ~__oLockBaseEmp();
    virtual __tBOOL Lock();
    virtual __tBOOL Unlock();
    __tBOOL IsLooked();
    __tlong GetLockCount();
    void EnableLocking(__tBOOL bLocking );
    };

    implementation for native windows

    __oLockBaseEmp::__oLockBaseEmp()
    {
    memset( &m_lock, 0, sizeof(LOCKOBJECTTYPE) );
    }
    __oLockBaseEmp::~__oLockBaseEmp()
    {
    if( m_lock.DebugInfo )
    ::DeleteCriticalSection(&m_lock);
    }
    __tlong __oLockBaseEmp::GetLockCount()
    {
    if( !m_lock.DebugInfo )
    return 0;
    return m_lock.LockCount*-1;
    //return m_lock.DebugInfo->ContentionCount;
    }
    __tBOOL __oLockBaseEmp::Lock()
    {
    if( !m_lock.DebugInfo )
    ::InitializeCriticalSection(&m_lock);
    ::EnterCriticalSection(&m_lock);
    return TRUE;
    };
    __tBOOL __oLockBaseEmp::Unlock()
    {
    ::LeaveCriticalSection(&m_lock);
    return TRUE;
    };
    __tBOOL __oLockBaseEmp::IsLooked()
    {
    return m_lock.DebugInfo && m_lock.RecursionCount > 0;
    }

    I want to code another implementation inside managed code

    Note: All locked and unlocked objects are unmanaged

    • Edited by Tamer Ismael Friday, July 19, 2019 7:03 AM adding code
    Friday, July 19, 2019 6:58 AM
  • Hello,

    Thank you for posting here.

    It seems that this lock class can help you. It can replace EnterCriticalSection() and LeaveCriticalSection() in your code. Here is the link for your reference.

    https://docs.microsoft.com/en-us/cpp/dotnet/lock-class?view=vs-2019

    Best Regards,

    Suarez ZHou

    • Marked as answer by Tamer Ismael Friday, July 26, 2019 6:46 PM
    Friday, July 19, 2019 9:00 AM
  • Hello,

    Do you resolve the issue? If you resolve the issue, could you please mark the helpful as answer. it will be beneficial to other community. If not, please feel free to contact us.

    Best Regards,

    Suarez Zhou

    Tuesday, July 23, 2019 3:15 AM
  • Hi

    Thanks a lot for everyone, I tried to use the same CriticalSection of native APIs and it worked fine. I had a problem of defining header files to work on dotNet. I  solved this problems last few days

    Thanks a lot

    Friday, July 26, 2019 6:48 PM