Implementation of multi-threading in C++builer

Source: Internet
Author: User
Tags thread

Also in the DOS era, people are looking for a multitasking implementation. Then there is a TSR type of background-resident program, more representative of the side Kick, Vsafe and other excellent TSR procedures, the emergence and application of such programs does give users a great convenience in the use of computers, such as side Kick, we can programming in the state without the editor, While editing the source program, while compiling and running, very convenient. However, the fatal flaw in the DOS single task operating system is doomed to not be able to develop a real multitasking program under DOS. Entering the Windows3.1 era, this situation is still not fundamentally changed, one application can only do something. For example, database queries, unless the application is well compiled, the entire system will not respond to user input during the query.

In the Windows NT and Windows 9x era, the situation has been radically changed, and the operating system has implemented multitasking in real sense (strictly speaking, Win9x). An application, when needed, can have many threads of execution, each thread is a small execution program, the operating system automatically allows each thread to share CPU resources, to ensure that no one thread can cause the system to deadlock. In this way, when programming, you can move the time-consuming task to the background, in the foreground with another thread to accept the user's input. For those programming tasks that require high real-time requirements, such as network customer service, serial communication and other applications, the implementation of multithreading will undoubtedly greatly enhance the usability and stability of the program.

In Windows NT and Windows 9x, multithreaded programming implementations need to invoke a series of API functions, such as CreateThread, ResumeThread, and more cumbersome and error-prone. We use Inprise's new generation of RAD development tools C++builder to easily implement multithreaded programming. With the old RAD Tools Visual Basic and Delphi, C++builer is not only very powerful, but its programming language is C + +, for the system development language is C's Windows series operating system, it has the incomparable advantage of other programming languages. With the TThread object provided by C++builder, multithreaded programming becomes very easy and easy to use. So, how do we do that? And wait for me slowly, let you experience the powerful function of multithreading.

1. Create multithreaded Routines:

First of all, first of all, introduce the specific steps to achieve multithreading. Although the concept of threads is illustrated with TThread objects in C++builder, the TThread object itself is not complete, it needs to create a new subclass under TThread, and overload the Execute method to use the thread object. This can be easily achieved under the C++builder.

Select a menu in the C++builder IDE environment file| New, select Thread object in the new column, press OK, then eject the input box, enter the name of the TThread object subclass Mythread, so that C++builder automatically creates a TThread subclass for you named Tmythread. At the same time there is an extra cell named Unit2.cpp in the editor, which is the original code for the Tmythread subclass we created, as follows:

#pragma hdrstop
#include “Unit2.h”
#pragma package(smart_init)
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
// Synchronize(UpdateCaption);
// where UpdateCaption could look like:
// void __fastcall MyThread::UpdateCaption()
// {
// Form1->Caption = “Updated in a thread”;
// }
__fastcall MyThread::MyThread(bool CreateSuspended)
: Tthread(CreateSuspended)
void __fastcall MyThread::Execute()
//---- Place thread code here ----

The Execute () function is where the code for the task we want to implement in the thread is located. With Unit2.cpp in the original code, we can use the Tmythread object we created. When used, dynamically creating a Tmythread object, using the Resume () method in the constructor, adds a new thread tmythread to our own definition, and the code that executes is the code of the Execute () method overload. To load more threads, it doesn't matter, as long as you continue to create the required number of Tmythread objects.

Above we have initially implemented a custom thread in the program and enabled the program to implement multi-threaded applications. However, the implementation of multi-threaded applications is not a simple task, but also need to consider many factors that enable multiple threads to coexist in the system, not affect each other. For example, access to public variables in the program, allocation of resources, if not handled properly, not only threads will deadlock into chaos, and may even cause system crashes. Generally speaking, in multithreaded programming should pay attention to the sharing of objects and data processing, can not be ignored. So what we're going to talk about here is a common problem with multiple threads:

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: 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.