The idea of encapsulating some simple functions of Boost: thread into Java threads.

Source: Internet
Author: User

Recently, I am interested in C ++, mainly because I want to write a database myself for fun. I did not expect that writing with C ++ is really a word: hard! (Personally, compared with Java)

I heard that the boost library is very good. The most important thing is that I have taken a fancy to its portability and I will try it out. Hey, it's a good stuff. It seems not very friendly to me who are used to Java. I was so angry that it took me a morning to encapsulate its thread library. As for efficiency and performance, I did not care about it. You can inherit this class and override the run method to create a new Thread class!

Header file:

# Pragma once
# Ifndef thread_h_mydb
# Define thread_h_mydb
# Include <boost/thread. HPP>
# Include <boost/Bind. HPP>
Namespace mydb {

Class thread
{
PRIVATE:
Typedef void (thread: * funptr) (void );
Boost: thread * thrd;
Public:
Thread ();
Void Init ();
Virtual ~ Thread (void );
Virtual void run ();
Void start ();
Void join ();
Boost: thread * getboostthreadptr ();
};

}
# Endif

Implementation file:

# Include "thread. H"
# Include "iostream"

Mydb: thread ::~ Thread (void)
{
}
Mydb: thread (void ){
// Init ();
Boost: function0 <void> F = boost: BIND (& Thread: Run, this );
Thrd = new boost: thread (f );
}
Void mydb: thread: Run (){
STD: cout <"this is base thread !! "<STD: Endl;
}
Void mydb: thread: Start (){

}
Void mydb: thread: Init (){


}
Void mydb: thread: Join (){
// The current todo thread will wait for the thread to finish and end.
Thrd-> join ();
}
Boost: thread * mydb: thread: getboostthreadptr (){
Return thrd;
}

When I was writing these codes, I posted some help posts on the csdn forum. I am very happy that someone quickly answered my questions and someone else saw that I was using Java .. Ah.

. Give yourself a toy.

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.