Skip to content

Instantly share code, notes, and snippets.

@melvyniandrag
Created November 1, 2017 17:39
Show Gist options
  • Save melvyniandrag/294d552296d1ab3679e5140bbac1a1a4 to your computer and use it in GitHub Desktop.
Save melvyniandrag/294d552296d1ab3679e5140bbac1a1a4 to your computer and use it in GitHub Desktop.
start p thread with static wrapper around non static function
#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