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.