add original source
This commit is contained in:
28
source/listing_3.11.cpp
Normal file
28
source/listing_3.11.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
struct some_resource
|
||||
{
|
||||
void do_something()
|
||||
{}
|
||||
|
||||
};
|
||||
|
||||
|
||||
std::shared_ptr<some_resource> resource_ptr;
|
||||
std::mutex resource_mutex;
|
||||
void foo()
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(resource_mutex);
|
||||
if(!resource_ptr)
|
||||
{
|
||||
resource_ptr.reset(new some_resource);
|
||||
}
|
||||
lk.unlock();
|
||||
resource_ptr->do_something();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
foo();
|
||||
}
|
||||
Reference in New Issue
Block a user