Encapsulation of Linux C ++ threads (Java-like style)

Source: Internet
Author: User

In Linux, the thread uses pthread. H, which isC LanguageTo be more object-oriented, I am writingProgramWhen encapsulated into a class, some internal libraries are used, but removing the internal library is also harmless. I am too lazy to change the program, so I have no internal header files or function calls.

Thread. h

/**
* @ File thread. h
* @ Version
* @ Date 2011/5/12
* @ Author wangyou@baidu.com
* @ Brief encapsulates threads in a simple way (Java style)
**/
# PragmaOnce

# Include

# Include//Thread

ClassThread {
Public:
/**
* @ Brief Constructor
* @ Param none
* @ Return none
**/
Thread (): _ ID (0){
}

/* *
* @ Brief start thread
* @ Param none
* @ Return none
* */
Void Start ();
/* *
* @ Brief thread sleep
* @ Param sleep (MS)
* @ Return none
* */
Static Void Sleep (uint32_t msec ); // MSEC milliseconds of sleep
/* *
* @ Brief get the thread ID
* @ Param none
* @ Return thread ID
* */
Pthread_t get_thread_id ();
/* *
* @ Brief: Wait for the thread to exit
* @ Param none
* @ Return none
* */
Void Wait_for_exit (); // Waiting for thread return
Protected :
Virtual Void _ Run () =0 ; // Thread Functions
Private :
Pthread_t _ id; // Thread ID
Static Void * Thread_func ( Void * ARGs );
};

Thread. h

# Include
# Include
# Include"Thread. h"

VoidThread: Start (){
IntRc = ul_pthread_create (& _ id, null, thread_func,This);
If(RC! =0){
Ul_writelog (ul_log_fatal,"Failed to create thread");
Exit (1);
}
}

pthread_t thread: get_thread_id () {
return _ id;
}

void thread: Sleep (uint32_t msec) {
struct timespec st;
St. TV _sec = msec/ 1000 ;
St. TV _nsec = (MSEC % 1000 ) * 1000 * 1000 ;
If (nanosleep (& St, null) =- 1 ) {
ul_writelog (ul_log_warning, " sleep failed " );
}< BR >}

void * thread: thread_func ( void * ARGs) {
thread * t = (thread *) ARGs;
T-> _ run ();
return NULL;
}

void thread: wait_for_exit () {
If (_ ID! = 0 ) {
If (ul_pthread_join (_ id, null )! = 0 ) {
ul_writelog (ul_log_fatal, " wait for the end of the thread to fail " );
}< span style = "color: # 0000ff; "> else {< br> _ id = 0 ;
}< BR >}

note that the ul_pthread_create file is used in the file and can be directly changed to pthread_create. The log Printing program will not be used. It is easy to use. It directly inherits the word Thread class and implements the run virtual method. When using it, you only need to call the start method. In fact, it can be completely unencapsulated, but it may make the program less readable.

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.