簡體   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