Windows multi-thread programming experience (preventing deadlock)

Source: Internet
Author: User

) Do not operate the MFC control in the thread function. Do not call the updatedata function in the thread to update the user interface. Instead, try to send messages, operate controls in the message response function of the main thread;

2) It is not recommended to use sendmessage to send messages to the main thread, because it is synchronous and congested. You can use postmessage instead;

3) when the thread exits, try not to use the terminatethread function, and try to let the thread quit on its own;

4) When a thread exits, it must wait for the worker thread to exit before the main thread exits. However, do not use waitforsingleobject or waitformultiobjects in the main thread to wait for the thread to end because it may cause a deadlock, when the main thread uses these two functions, the main thread is suspended, especially in the (1) and (2) cases, the worker thread is still calling Resources in the main thread, this causes a deadlock;

5) To prevent deadlock, try to use the msgwaitformultipleobjects function, because the function can wait for the thread handle to have a signal while waiting, and wait for the message without causing a deadlock;

 

Original address: http://software.intel.com/zh-cn/blogs/2011/08/03/windows-2? Cid = Sw: prccsdn1945

Related Article

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.