2017-05-28 4 views
0

私の仕事は間伐アルゴリズムの処理を高速化することになっている画像処理プロジェクトに取り組んでいます。関数のメンバを持つstructポインタを正しく渡す方法

これは、最初のコードの抜粋である: -

#include <iostream> 
#include <stdlib.h> 
#include <deque> 
#include <opencv2/imgproc/imgproc.hpp> 

#define ROWS 10 
#define COLS 10 
class Thinner 
{ 
protected: 
    typedef bool (*Thinner_Function)(uchar* skeletal_data, int iter, int col, 
      int row, int cols); 
    bool thin_customize(Thinner_Function thin_fn); 
    std::deque<int> cols_to_set; 
    std::deque<int> rows_to_set; 
}; 

bool Thinner::thin_customize(Thinner_Function thin_fn) 
{ 
    uchar *skelcontour_data; //some data... 
    for (unsigned short iter = 0; iter < 2; ++iter) 
    { 
     // for each point, check if it needs to be changed 
     for (int row = 0; row < ROWS; ++row) 
     { 
      for (int col = 0; col < COLS; ++col) 
      { 
       if (thin_fn(skelcontour_data, iter, col, row, COLS)) 
       { 
        cols_to_set.push_back(col); 
        rows_to_set.push_back(row); 
       } 
      } // end for (col) 
     } // end for (row) 
    } 
} 

私はそれがスピード物事を助けかどうかを確認するために2つの別々のスレッドに

cols_to_set.push_back(col); 
rows_to_set.push_back(row); 

を分割したいです。だからここ

は私が試したものです: - プログラムの場合

class Thinner 
{ 
protected: 
    typedef bool (*Thinner_Function)(uchar* skeldata, int iter, int col, int row, int cols); 
    typedef bool (*My_Thinner_Function)(uchar* skeldata, int iter, int col, int row, int cols); 
    void *push_rows(void *args); 
    void push_cols(void *args) 
    bool thin_customize(); 
    std::deque<int> cols_to_set; 
    std::deque<int> rows_to_set; 
    struct thread_struct 
    { 
     unsigned short iter; 
     Thinner_Function thin_fn; 
    }; 

}; 

void Thinner:: *push_cols(void *args) 
     { 
    Thinner::thread_struct *thread_data = args; 
    //unsigned short iter = thread_data->iter; 

    //Thinner::Thinner_Function thin_fn = thread_data->thin_fn; 
    //this is incorrect...gives seg faults. 
    //how to pass a function via struct in pthread_create()? 

    for (int row = 0; row < ROWS; ++row) 
    { 
     for (int col = 0; col < COLS; ++col) 
     { 
      if (thin_fn(skelcontour_data, iter, col, row, COLS)) 
      { 
       cols_to_set.push_back(col); 
      } 
     } 
    } 
     } 

// similar for rows... 

bool Thinner::thin_customize() 
{ 
    pthread_t thread1, thread2; 
    thread_struct thread_data = {0, My_Thinner_Function}; 
    for (unsigned short iter = 0; iter < 2; ++iter) 
    { 
     rows_to_set.clear(); 
     cols_to_set.clear(); 
     int iret1, iret2; 
     // for each point in skelcontour, check if it needs to be changed 
     iret1 = pthread_create(&thread1, NULL, push_cols, &thread_data); 
     if (iret1) 
     { 
      fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1); 
      exit(EXIT_FAILURE); 
     } 
     iret2 = pthread_create(&thread2, NULL, push_rows, &thread_data); 
     if (iret2) 
     { 
      fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2); 
      exit(EXIT_FAILURE); 
     } 
     pthread_join(thread1, NULL); 
     pthread_join(thread2, NULL); 
    } 
} 


but I'm stuck at not being able to properly pass the 

    typedef bool (*Thinner_Function)(uchar* skeletal_data, int iter, int col, int row, int cols) 

function via a struct through `pthread_create()` 


    EDIT: based on suggestions by @G.M. and @Shadowigor:- 

I tried to initialize the function inside `thin_customize()`, but I'm not sure how to do that. Sorry for the delay in response. Have been trying to init. the `Thinner_Function()` all day today, to no avail. 
+0

'Thinner :: thin_customize'で' thread_data'をどこで初期化しますか? –

+0

なぜC++の標準スレッドライブラリを使用していないのですか? –

+1

pthreadsの代わりにstd :: asyncを使用します。 –

答えて

0

それがあるとして、あなたはthread_data->thin_fnは、このようにセグメンテーション違反、無効になります理由である、thread_dataを初期化することはありません。

関連する問題