class.cpp
#include "class.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int haha::set_tmp(int value){
tmp = value;
return 0;
}
int counter = 0;
void *thread_test(void *para){
haha *ptr = (haha *)para;
pthread_detach(pthread_self());
while(1){
ptr->set_tmp(counter++);
printf("haha = %d\n", ptr->tmp);
sleep(1);
}
pthread_exit(0) ;
}
void haha::create_thread(){
pthread_t thread;
pthread_create(&thread , NULL , thread_test , this);
}
int main(void){
haha test;
test.tmp = 5;
test.create_thread();
while(1){
sleep(4);
}
return 0;
}
class.h
class haha{
public:
int tmp;
void create_thread();
int set_tmp(int value);
};
g++ class.cpp -o test -lpthread
https://docs.google.com/file/d/0B8hm-I2M8BD7TF9UTnJkLU1aa2s/edit?usp=sharing
沒有留言:
張貼留言