Talk about thread BASICS (answer Wang Liwei)

Source: Internet
Author: User

(Wang Liwei brother design line based Class Original address: http://blog.csdn.net/wei801004/archive/2006/05/18/744341.aspx)

In the previous trackback article, the thread-based category designed by Brother Wang put forward some personal opinions. Brother Wang then replied:" Thanks to seawave for your comments. The best way to terminate a thread is to let the thread exit after the task is executed, and all we need to do is closehandle. There is also static DWORD winapi threadfun (lpvoid lpparameter); this function can be static, we can call cthread: threadfun in this way, is the declaration of private and public different? ". I will add the following: First of all, Wang Xiong's thread-based class is well designed. Using the "template method" makes it easier to use the thread. Code Obviously it's just an example, so it's easy to write. I'm just nitpicking ......
  1. Why do I want to declare threadfun private instead of public?
    Because this function should never be called by customersProgramIt should be a hidden private function, because it is only a function entry for Windows callback, And the callback function should never be called directly. Of course, you can also say that although it is a callback function, if we call it directly, there will be no problem ...... In fact, there are still some problems. First of all, too many public declarations make programmers of your class more burdened. They must figure out how to use this public function (since you have made it public, there must be a public reason). Secondly, if someone else calls this function directly, the caller's thread will block until threadworkitem () returns. This is definitely not what we want, right?
  2. Why is there a problem with the stop () function?
    Indeed, the best way to terminate a thread is to let the thread exit. However, the above design is either a problem with the function name stop or a problem with the implementation of stop. Any classic design book tells us,The function name must be used to obtain the actual operations that match the function name.Since the function name is stop, others who use this class will first assume that calling this function will terminate the thread, in fact, it simply closes the thread handle. On the other hand, if it only does this thing, why don't we close the handle when the thread is just created? Why do we create a public method for closehandle?
    In addition, since the stop function closes the handle, the handle is no longer available, so we should set this variable to null, and to set it to null, this function cannot be a const function.
  3. What will I do?
    Obviously, the STOP () function should be "person as its name", but it cannot terminatethread, and cannot be blocked until the thread ends. Therefore, I will first change the function name, which can be called setstopflag (). Its function is simply to set a flag, and the user thread is usually a loop, this sign will be judged at the right time to determine whether it should be terminated ...... This method can be seen in the tthread class in Delphi. Its terminate process simply sets the terminated flag;
    Second, I will add a wait function, which can call waitforsingleobject to wait for the thread to terminate. This function is quite useful to client programmers (in fact, in my implementation, there is also a function named stopandwait ).

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.