peter55555
peter55555

Reputation: 1475

Can std::lock_guard be interupted?

If I have some code under lock_guard like:

std::thread t([&]()
{
    std::lock_guard<std::mutex> lock(m);
    // some simple operations
});

Do I have a guarantee that "some operations" will never be interrupted on this core cpu that t thread is running? Can there be context switching after lock_guard?

Upvotes: 1

Views: 267

Answers (2)

Brian Bi
Brian Bi

Reputation: 119219

No, a critical section protected by a mutex is not going to run with real-time priority unless you explicitly request it to do so, which cannot be done using the standard C++ library. The kernel can still schedule another thread (in the same process, or some other process) that isn't waiting on the mutex. Setting real-time priority can only be done with OS-specific system calls.

Upvotes: 0

Ahmad Siavashi
Ahmad Siavashi

Reputation: 999

There will be context switches but it is guaranteed that all other threads will be blocked behind the lock.

Upvotes: 1

Related Questions