繁体   English   中英

C++ 多线程

[英]C++ Multi-thread

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

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);
        }
}

它显示错误:没有用于调用“pthread_create”的匹配函数。

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

#include <pthread.h>

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

使用-pthread编译和链接。

我认为你最好让你的真正的工人功能是静态的:

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM