Article 49: Understanding New-handle Behavior

Source: Internet
Author: User

The memory management under multithreading is completely different from a single thread, because heap is a resource that can be changed globally, so all threads have the possibility to access this resource, which leads to a lot of race_conditions.

When operator new does not acquire the desired memory, it invokes a user-specified handler, New_handler. This function can be specified using Set_new_handler.
1 namespace std{2     void (*new_handler) (); // no parameters and return values 3     Throw (); 4 }
When operator new fails to meet the requirements of the memory application, it calls the New-handler function constantly, so a well-designed New-handler should meet the following requirement:allow more memory to be usedinstall another New-handler when the current handler is unable to meet the conditions.
remove New_handler and pass a null pointer to Set_new_handler, at which point operator new throws an exception when memory cannot be allocated
throws a Bad_alloc exception: Such an exception will be propagated to the memory of the requesting place
do not return: Call abort or exit
  when it is necessary to deal with different class memory allocation failures in different ways, you should provide your own set_new_handler and operator_new for the corresponding special class. This way, operator new will make sure to use class-specific new_handler instead of global new handler when allocating memory. A simple example of a widget allocating resources:
1 classnewhandlerholder{2  Public:3     ExplicitNewhandlerholder (std::new_handler NH)4 : Handler (NH) {}5~Newhandlerholder ()6Std::set_new_handler (handler);//restore state to a previous state7 Private:8 New_handler handler;9Newhandlerholder (ConstNewhandlerholder &);//Do not disable these two operationsTenNewhandlerholder &operator(ConstNewhandlerholder &); One};

The above can be used by a custom class:

 1  void  * widget::operator  new  (std::size_t size) throw   (Std::bad_alloc)  2  { 3  newhandlerholder H (Std::set_new_handler (Currenthandler)); //  4  return :: operator  new  (size); //  using the custom new  5 } 

In fact, the above Newhandlerholder can be designed as a base class template, so that Derived_class technology inherits the Set_new_handler and operator_new that they need, The template is used to ensure that each derived_class obtains an Currenthandler member variable that is an entity that is mutually dissimilar. Below for this template:

1Template<typename t>//here The TypeName T is not used, the function below will say2 classnewhandlersupport{3  Public:4     StaticStd::new_handler Set_new_handler (Std::new_handler p)Throw();5     Static void*operator New(std::size_t size)Throw(std::bad_alloc);6     ...7 Private:8     StaticStd::new_handler Currenthandler;9 };TenTemplate<typename t> One StaticStd::new_handler ANewhandlersupport<t> Set_new_handler (Std::new_handler p)Throw() - { -Std::new_handler Oldhandler =Currenthandler; theCurrenthandler =p; -     returnOldhandler; - } -Template<typename t> + Static void* -Newhandlersupport<t>::operator New(std::size_t size)Throw(Std::bad_alloc) + { A Newhandlerholder H (Std::set_new_handler (Currenthandler)); at     return::operator(size);//call the global operatornew to prevent recursive calls to itself.  -}

With this template, it's easy to have a dedicated set_new_handler and operator for widgets. Inherit the above template and make it special:

1 class  Public Newhandlersupport<widget>{2    ... 3 };
This will have the exclusive Set_new_handler and operator new. The TypeName T is not used and is not actually needed, and the purpose of this t is simply to make different instances of the subclass that inherit from Newhandlersupport have different instance functions. (mainly for static variables, Currenthandler). T is only used to distinguish between different derived_class. (Because the above T is instantiated as a widget) and there is a problem with the nothrow, using the new (Std::nothrow) widget does not produce an exception on new, only a null pointer is returned, but if new is normal, it is possible to create an exception during the construction process. So there is still no exception guarantee. Nothing to do with new (Std::nothrow) sth; Summary:Set_new_handler allows the user to specify a function to invoke when memory is not allocatedNothrow new can only guarantee that new does not throw exceptions, and constructs are still not guaranteed.

Article 49: Understanding New-handle Behavior

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.