C ++ builder multi-thread download operations

Source: Internet
Author: User

First, declare the global variables and critical value TCriticalSection * CS;
// Global Zero-Boundary Value
Declare the thread in the header file or new file (actually a class)
// Download Thread class TMyThread: public TThread {public :__ fastcall TMyThread (void );
Private: virtual void _ fastcall Execute (void );
};
Set the Declaration in CPP // download the thread declarative _ fastcall TMyThread: TMyThread (void): TThread (true)
{FreeOnTerminate = true;
// This is automatic recovery of Resume ();
// This indicates execution.
Execute the code in Execute ()
}
// Download the thread execution body void _ fastcall TMyThread: Execute ()
// Thread function {CS-> Enter ();
// Various operations with critical values + If any operation is used globally, the operation is placed in the middle of the critical value. If not, the operation can be put out and executed CS-> Leave ();
// Critical value}
Enable the thread: you only need to create a NEW thread, because automatic recycle is used, so there is no recycle operation. If manual recycle is used, you must pay attention to it.
// Enable the download thread TMyThread * MyThread = new TMyThread ();
MyThread-> FreeOnTerminate = true;
Note: It is best not to use NEW or the like in the thread, because the thread is destroyed after use. So pay attention


This is an excerpt from my blog

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.