Created
November 1, 2017 17:39
-
-
Save melvyniandrag/294d552296d1ab3679e5140bbac1a1a4 to your computer and use it in GitHub Desktop.
start p thread with static wrapper around non static function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <iostream> | |
#include <map> | |
#include <set> | |
#include <vector> | |
#include <pthread.h> | |
class Base{ | |
public: | |
int x; | |
Base(int x_): x(x_){} | |
virtual ~Base(){} | |
void myFunc( ) | |
{ | |
int N = 10000000; | |
for ( int i = 0; i < N; ++i ) | |
{ | |
++x; | |
} | |
} | |
static void *wrapper( void *VoidBaseInstance ) | |
{ | |
Base* BaseInstance = static_cast<Base *>( VoidBaseInstance ); | |
BaseInstance->myFunc(); | |
} | |
}; | |
int main(int argc, char** argv){ | |
Base b(0); | |
pthread_t t; | |
pthread_create( &t, NULL, &Base::wrapper, &b ); | |
pthread_detach( t ); | |
b.myFunc(); | |
sleep(2); | |
std::cout << b.x << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment