TB-common-utils source code analysis (2): simple thread management

Source: Internet
Author: User

In the previous article, we studied the implementation of log classes. It is relatively simple and practical. This time, we will study a simple thread management solution.

Before analyzing the source code, let's take a look at an example of using the Thread class. The Code is as follows:

 

# Include <stdio. h> <br/> // reference header file <br/> # include <tbsys. h> <br/> using namespace tbsys; <br/> // write the inherited Thread class <br/> class myrunable: public runnable {<br/> // here is our thread function <br/> void run (cthread * thread, void * Arg) {<br/> printf ("thread run/N"); <br/>}< br/>}; <br/> int main () {</P> <p> // create our Thread class <br/> myrunable test; </P> <p> // create a thread management instance <br/> cthread thread; <br/> // start a thread <br/> thread. start (& test, null); </P> <p> // wait for the thread to exit <br/> thread. join (); </P> <p> return 0; <br/>} 

In the code, we first declare a class named myrunable, which inherits from runnable. Runnable is a pure virtual class. Only one run method needs to be implemented by sub-classes.

Then we create a cthread instance, which is used to manage a thread. The start method is used to start a thread. This method has two parameters. One is an instance inherited to runnable, one is the parameter to be passed to our thread. No parameter is passed here. It is set to null first, and the join method is used to add the current thread to wait.

We can see that we can easily create and manage threads.

Let's take a look at how this is implemented.

Let's take a look at the runnable class. This class is relatively simple. It is a pure virtual class and only provides one pure virtual function:

Class runnable {<br/> Public: <br/>/* <br/> * structure <br/> */<br/> virtual ~ Runnable () {<br/>}< br/>/** <br/> * run the entry function <br/> */<br/> virtual void run (cthread * thread, void * Arg) = 0; <br/> }; 

Let's take a look at the implementation of the cthread class. This class is mainly responsible for thread management, querying the thread ID and passing the thread parameters. Let's first look at other member variables:

PRIVATE: <br/> pthread_t tid; // pthread_self () ID <br/> int PID; // thread process id <br/> runnable * runnable; <br/> void * ARGs; 

Let's look at the main member functions. The start function is used to start a thread:

Void start (runnable * r, void * A) {<br/> runnable = r; <br/> ARGs = A; <br/> // create a thread, the thread function is a hook and passes it to the function <br/> pthread_create (& tid, null, cthread: hook, this); <br/>} 

This function does not pass a real thread function to pthread_create, but the hook function to it. In this way, we can break through the limitations that require passing static member functions.

Static void * hook (void * Arg) {<br/> // obtain the Thread class <br/> cthread * thread = (cthread *) ARG; <br/> thread-> pid = gettid (); <br/> If (thread-> getrunnable ()) {<br/> // call the thread running class <br/> thread-> getrunnable ()-> Run (thread, thread-> getargs ()); <br/>}< br/> return (void *) NULL; <br/>} 

In the hook function, we can clearly see that the function first sets the PID, and then calls the run function in our class.

Through thread encapsulation, we can target the thread, and we can easily control and manage the thread.

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.