C ++多线程

时间:2015-09-14 03:00:21

标签: c++ multithreading

在Class中,当我尝试为这样的方法创建一个线程时:

void *RippleBrush::paintRippleOnce(void){
    while(1){
        for (int j = 0; j < height; j ++) {
            for(int i = 0; i < width; i ++){
                int point = j * height + i;
                data[point].a += ripple->rippleNow[point];
                ripple->CaculateNextRipple();
            }
        }
    }
}

void RippleBrush::paintRipple(){
    pthread_t ctrl_thread;
        if(pthread_create(&ctrl_thread, NULL, RippleBrush::paintRippleOnce, NULL) != 0){
            perror("pthread_create");
            exit(1);
        }
}

显示错误:没有匹配函数来调用&#39; pthread_create&#39;。

如何在一个方法中为同一个类中的另一个方法创建一个线程?

2 个答案:

答案 0 :(得分:0)

#include <pthread.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 
                   void *(*start_routine) (void *), void *arg);

编译并与-pthread

相关联

答案 1 :(得分:0)

我认为你最好让你的真正的工作者作为静态函数:

void *RippleBrush::paintRippleOnce(void){
    while(1){
        for (int j = 0; j < height; j ++) {
            for(int i = 0; i < width; i ++){
                int point = j * height + i;
                data[point].a += ripple->rippleNow[point];
                ripple->CaculateNextRipple();
            }
        }
    }
}

void RippleBrush::paintRipple(){
    pthread_t ctrl_thread;
        if(pthread_create(&ctrl_thread,NULL, RippleBrush::paintRippleOnceWrapper,this)!=0){
            perror("pthread_create");
            exit(1);
        }
}

static void* RippleBrush::paintRippleOnceWrapper(void *args) {
   RippleBrush* brush= (RippleBrush*)args; // or dynamic_cast as you like
   brush->paintRippleOnce();
}