Current Method(s) to lock/unlock a range in a native array for read or write using

mwx 41 Reputation points
2020-10-28T19:32:44.093+00:00

What would be a good and efficient way to have multiple threads and processes to read or write from a range of a RWS unsigned char array when they seek to read or write a contiguous range? I already have the array in a dll in shared memory, so the question is only about locking? I was looking at the ConCRT but have not found anything yet that works.

const long lbuf = 0x10000;
#pragma data_seg(".sdataRWS")
#pragma comment(linker, "/Section:.sdataRWS,RWS")
//...
volatile  unsigned char ucbuf[lbuf];
//...
#pragma data_seg()
//  dll functions handle requests to read or write from clients
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 9,181 Reputation points Microsoft Vendor
    2020-10-29T02:40:05.37+00:00

    Hi,

    According to the issue, I wonder If you want to protect a shared resource from simultaneous access by multiple threads or processes? If so, I suggest you could try to use a mutex Class. Each thread must wait for ownership of the mutex before it can execute the code that accesses the shared resource.

    I suggest you could try to construct a mutex object. by std::mutex, and then call the member function lock() to lock the mutex, call the member function unlock() to unlock it.

    You could also try to use lock_guard Class.

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.