The multithreading technique of C + + in class under Linux

Source: Internet
Author: User

There are times when we want to use multithreading techniques in C + + classes for more time-consuming functions, but the function pointers for member functions of C + + classes cannot be directly passed as parameters to pthread_create, mainly because C + + member function pointers have class namespaces. At the same time, the end of the member function is the C + + compiler plus the this pointer parameter that can receive the object's address. Therefore, it is necessary to convert the member function into a non-compiler plus this pointer, and we will maintain the "this" pointer for the function by ourselves.

#include <stdio.h>#include<stdlib.h>#include<iostream>#include<time.h>using namespacestd;classtest{ Public:        intsum=0; intCNT;  Public:        intinsert ();};intTest::insert () {Sleep (2); CNT+=1;}

As shown above, the code declares a class test, assuming that the class has a very time-consuming member function: Insert (), which takes 2000ms of time each time the function executes. For such a time-consuming function, we would like to design the method to design it as a thread function, so that the caller does not block.

So we add multithreading to it:

#include <stdio.h>#include<stdlib.h>#include<iostream>#include<time.h>#include<unistd.h>#include<pthread.h>using namespacestd;classtest{ Public:        intsum=0; intCNT;  Public:        intInsert (); void* Insert_pth (void*); voidlanch ();};intTest::insert () {Sleep (2); Sum+=1;}void* Test::insert_pth (void*) {insert ();}voidTest::lanch () {pthread_t pth; Pthread_create (&pth,null,insert_pth,null);}intMain () {Test T;    T.lanch (); return 0;}

The above code creates multithreading by calling Lanch () to execute insert_pth,insert_pth and then calls Insert (). However, this code will error when compiling:

Simply change the insert_pth to the static function and transfer the insert logic code to the insert_pth:

#include <stdio.h>#include<stdlib.h>#include<iostream>#include<time.h>#include<unistd.h>#include<pthread.h>using namespacestd;classtest{ Public:        intsum=0; intCNT;  Public:        intInsert (); Static  void* Insert_pth (void*); voidlanch ();};void* Test::insert_pth (void*__this) {Test* _this = (Test *) __this; Sleep (2); _this->sum+=1; printf ("%d insert.....\n",_this->sum);}voidTest::lanch () {pthread_t pth; Pthread_create (&pth,null,insert_pth, (void*) This);}intMain () {Test T; T.sum=0;    T.lanch (); Sleep (5); return 0;}

Attention:

To use multithreading to process time-consuming member functions:
1. Declare another static function: static void xxx_pth (void __this);
This function is consistent with the target member function in the function name as much as possible.

2. Copy the Code of the original member function to void * xxx_pth (void * __this);
A pointer objectpoint _this that converts void * __this to an object at the beginning of xxx_pth (); Add all the copied member variables to _this->

3. Write the thread startup code.
Pthread_create () The last parameter passed in the this pointer

4. You need to add the-lpthread option when compiling.  

The multithreading technique of C + + in class under Linux

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.