Skip to main content

Posts

Showing posts from November, 2017

POST 1 : std::thread in c++ | C++ 11

std::thread   C++  Thread support library std::thread. Threads enable programs to execute across several processor cores. Defined in header Class : thread Member types : native_handle_type Data Member  id    //represents the id of a thread (public member class) Member functions (constructor)    //constructs new thread object (public member function) (destructor)    //destructs the thread object, underlying thread must be joined or detached  (public member function) operator=    //moves the thread object  Observers  // Covered in NEXT POST Operations    // Covered in NEXT POST Constructor :   #include   void f1 ( int n ) { for ( int i = 0 ; i < 5 ; ++ i ) { std:: cout << "Thread 1 executing \n " ; ++ n ; std:: this_thread :: sleep_for ( std:: chrono :: milliseconds ( 10 ) ) ; ...

weak_ptr in C++ | | C++ 11

weak_ptr : A weak_ptr is created as a copy of shared_ptr. It provides access to an object that is owned by one or more shared_ptr instances, but does not participate in reference counting. The existence or destruction of weak_ptr has no effect on the shared_ptr or its other copies. It is required in some cases to break circular references between shared_ptr instances. Cyclic Dependency (Problems with shared_ptr): Let’s consider a scenario where we have two classes A and B, both have pointers to other classes. So, it’s always be like A is pointing to B and B is pointing to A. Hence, use_count will never reach zero and they never get deleted. A cycle occurs when two or more resources controlled by shared_ptr objects hold mutually referencing shared_ptr objects. For example, a circular linked list with three elements has a head node N0 ; that node holds a shared_ptr object that owns the next node, N1 ; that node holds a shared_ptr object that owns the next node, N2 ; that n...

shared_ptr in C++ | C++ 11

shared_ptr : Definition: Wraps a reference-counted smart pointer around a dynamically allocated object. Syntax template class shared_ptr;  The shared_ptr class defines an object that uses reference counting to manage resources. A shared_ptr object effectively holds a pointer to the resource that it owns or holds a null pointer. A resource can be owned by more than one shared_ptr object; when the last shared_ptr object that owns a particular resource is destroyed, the resource is freed. A shared_ptr stops owning a resource when it is reassigned or reset. Reference Counting:  It is a technique of storing the number of references, pointers or handles to a resource such as an object, block of memory, disk space or other resources. An object referenced by the contained raw pointer will not be destroyed until reference count is greater than zero i.e. until all copies of shared_ptr have been deleted. So, we should use shared_ptr when we want to assign one raw pointe...

std::unique_ptr in c++ | C++ 11

std::unique_ptr : C++11 introduces std::unique_ptr, defined in the header . It is a container for a raw pointer. A unique_ptr explicitly prevents copying of its contained pointer (as would happen with normal assignment), but the std::move function can be used to transfer ownership of the contained pointer to another unique_ptr. Reason for it won’t allow copy : A unique_ptr cannot be copied because its copy constructor and assignment operators are explicitly deleted. std::unique_ptr p1(new int(5)); std::unique_ptr p2 = p1; //Compile error. std::unique_ptr p3 = std::move(p1); //Transfers ownership. p3 now owns the memory and p1 is set to nullptr. p3.reset(); //Deletes the memory. p1.reset(); //Does nothing. Complate Example:  // C++ program to illustrate the use of unique_ptr #include #include using namespace std; class A { public :      void show()      {   ...