Skip to content

Instantly share code, notes, and snippets.

@RyosukeMiyahara
Created March 12, 2014 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyosukeMiyahara/9507711 to your computer and use it in GitHub Desktop.
Save RyosukeMiyahara/9507711 to your computer and use it in GitHub Desktop.
C++で、pthread_createで作ったスレッドから、クラスのstaticではないメンバ関数を呼びます。
#include<stdio.h>
#include<pthread.h> // pthread_create
#include<unistd.h> // sleep
extern "C" {
typedef void* (*ThreadFunc_t)(void*);
}
class Test {
private:
int number;
public:
Test() {
number = 5;
}
~Test() {
}
// static function to call test_function
static void* thread_function(void *arg) {
Test* test = (Test*)arg;
test->test_function();
return NULL;
}
// Target class member method
// We want to call this function from pthread
void* test_function() {
printf("Called successfully, number = %d\n", number);
return NULL;
}
};
// Call class member function from pthread
int main(void) {
Test test;
pthread_t thread;
pthread_create(&thread, NULL, (ThreadFunc_t)Test::thread_function, (void *)&test);
sleep(1); // Wait for pthread to call target function
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment