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