Is this code thread safe?

I am writing code in which a UI thread should interact with a background thread that is performing network communication. The code works, but will it be considered thread safe?

I would feel much better if someone experienced could lead me to the right path on this ...

static Mutex^ mut_currentPage = gcnew Mutex;
static array<unsigned char>^ m_currentPage;

property array<unsigned char>^ Write
{
    void set(array<unsigned char>^ value) 
    {
        mut_currentPage->WaitOne();
        m_currentPage = value;
        mut_currentPage->ReleaseMutex();
    }
}

This is .NET C ++ code ... :)

+3
source share
2 answers

It looks thread safe, but you might consider handling exceptions; setting a field should not be an error (except possibly ThreadAbortException), but if the code was more complex, you would like you to release the mutex after the exception.

Monitor ( "lock" #)

: , . string, ?

+4

, Monitor Mutex - , .

- , , , . volatile ( , # - , ++/CLI.)

+3

Source: https://habr.com/ru/post/1702867/


All Articles