Program Crash debug record and summary

Source: Internet
Author: User

1. Multi-threaded access to shared resources is not locked

Error Tip: Segmentation fault!
Workaround:

    • Multi-threaded read no need to lock
    • Multi-threaded simultaneous read and write requires lock
      The method of locking is the event notification mechanism provided by the combination of mutex, semaphore, read-write lock, record lock, mutex and condition variable.
      C++11, Lock_guard,unique_lock. Both are locked at the time of construction, and when the destruction is locked. But Unique_lock provides more features, such as the ability to specify that it is not locked at construction time, and then called when needed. Lock () lock. If combined with condition_variable, use only unqiue_lock.
2. The pointer variable is not initialized to nullptr at the time of definition, or at some point execution becomes a dangling pointer, causing unpredictable errors.

Error tip: Segmentaion fault!
Workaround:

    • Pointer variables are initialized to nullptr at the time of declaration, and when used to determine if (!PTR)
    • Check the code carefully to avoid hanging hands.
3. Std::thread in joinalble state causes terminate

Error message: Terminate called without an active exception. Aborted
Workaround:

    • Call detach to detach thread when creating thread
    • Call join after thread executes to wait for thread to end normally
      However, after writing a lot of code, sometimes in a code branch prematurely exit and cause thread destruction without running to the join statement, which will lead to terminate;
      The sample code is as follows:
  1. void foo()
  2. {
  3. //do something;
  4. }
  5. int main()
  6. {
  7. std::thread t(foo);
  8. if(true)return1;
  9. t.join();
  10. return0;
  11. }

Code 8 o'clock T constructs and runs with Foo, this is for the joinable state, the Code 9 o'clock program exits, causing T to be refactored before T.join (), and thread will cause terminate under joinable state destruction. The ~thread function is described in Cplusplus as follows:
If the thread is joinable -Destroyed, terminate () is called.



From for notes (Wiz)



Program crashes debug logging and summary

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.