What special purpose does a unique block using a mutex have?

I'm not quite sure why it std::unique_lock<std::mutex>is only useful when using regular locking. An example of the code I'm looking for is:

{//aquire lock

        std::unique_lock<std::mutex> lock(queue_mutex);

        //add task
        tasks.push_back(std::function<void()>(f));

}//release lock

why would it prefer more

queue_mutex.lock();

//add task
//...

queue_mutex.unlock();

do these code snippets do the same thing?

+4
source share
2 answers

[Do] do these pieces of code do the same thing?

No.

, , . , break, continue, return, goto, , .

+16

unique_lock .

  • , "" (, return)
  • ...

.

, , . , .


: ++, SBRM (Scoped Bound Resources Management), , , , /ungraceful exit, .

RAII ( ), unique_lock ( ). , , , SBRM, , , .

+4

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


All Articles