How to Implement multithreading in C ++

Source: Internet
Author: User

In Java, multithreading can be implemented in two ways: one is inherited from the Thread class, the other is runnable, and the other is the cwinthread class in MFC, but how can we use the existing multi-threaded mechanism to implement a multi-threaded class?

In Windows, we can use the C language for multi-threaded programming. One is createthread (), which is a Win32 API function and the other is _ beginthread (), this function is a CRT (C Run-Time) function. There is no big difference between the two functions. In this article, I will choose to use _ beginthread ().Process. hFile. The prototype of this function is
Uintptr_t _ beginthread (
Void (_ cdecl *Start_address) (Void *),
UnsignedStack_size,
Void *Arglist
);
The first parameter specifies the starting position of the thread. The second parameter specifies the size of the stack. The default value is 0. The third parameter specifies the parameter list transmitted to start_address. According to the definition of multi-threaded classes in Java, for the multi-threaded classes implemented in C ++, I will use the following statement:
Class thread
{
Public:
Void start ();
Virtual void run ();
}
As long as you inherit the class and override the void run () method, you can start the START () method to implement the multi-thread idea. So I implement the above two methods in this way:
Void thread: Start ()
{
_ Beginthread (run, O, null );
}
Void thread: Run ()
{
Cout <"base thread" <Endl;
}
The result compilation fails and is displayed.RunAnd_ Cdecl *) (void *) Type does not match, so naturally think of forced type conversion, but no matter what method is used is not successful, why? For this purpose, in the member functions of the class, each function has a default parameter this, and this parameter is passed in implicitly, so I didn't succeed (I am still a little confused about this. I hope you can answer it .) Later, I discussed this issue with my classmates. It was a good idea to define a static method inside the class to solve this problem. So I changed the declaration of the thread class:
Class thread
{
Public:
Void start ();
Virtual void run ();
Handle getthread ();
PRIVATE:
Handle hthread;
Static void agent (void * P );
};

The agent method is the biggest improvement in this class. It is a static method, but it is declared as a private method and can only be called internally, the entire class starts from start and then runs run () by creating a new thread. The specific implementation of the entire function is as follows: (complete code, in Visual Studio 2005, windows XP SP2 pass the test), note that, select C/C ++ in project-> Properties and select multi-threaded debug (/MTD) in Runtime Library in code generation)
/*
* Thread. h
*/
# Ifndef thread_h
# Define thread_h
# Include <process. h>
# Include <iostream>

Using STD: CIN;
Using STD: cout;
Using STD: Endl;

Typedef void * handle;
Class thread
{
Public:
Void start ();
Virtual void run ();
Handle getthread ();
PRIVATE:
Handle hthread;
Static void agent (void * P );
};

Void thread: Start ()
{
Hthread = (handle) _ beginthread (agent, 0, (void *) This );
}
Void thread: Run ()
{
Cout <"base thread" <Endl;
}
Void thread: Agent (void * P)
{
Thread * AGT = (thread *) P;
AGT-> Run ();
}
Handle thread: getthread ()
{
Return hthread;
}
# Endif // thread_h
Test File
# Include "thread. H"
# Include <windows. h>
# Include <iostream>
Using STD: CIN;
Using STD: cout;
Using STD: Endl;

Class derivedthread: Public thread
{
Public:
Void run ();
};
Void derivedthread: Run ()
{
Cout <"derived thread" <Endl;
}
Int main (INT argc, char * argv [])
{
Derivedthread * dt = new derivedthread ();
DT-> Start ();
Waitforsingleobject (DT-> getthread (), infinite );
}
Run display:
Derived thread
Successful!

Note: many people may run the _ beginthread command, which is not defined. In fact, you only need to modify the settings -- multithread debug

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.