2017-05-28 21 views
0

我工作的圖像處理項目中,我的工作就是加快細化算法的處理結構指針。如何正確地傳遞其在C中在pthread_create函數成員++

這是最初的代碼片段: -

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

我想將

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

分成2級獨立的線程,看它是否有助於加快速度。

因此,這裏是我的嘗試: -

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

你在哪裏初始化'更薄:: thin_customize''thread_data'? –

+0

爲什麼不使用C++的標準線程庫? –

+1

使用std :: async而不是pthreads。 – 2017-05-28 11:57:38

回答

0

對於程序,因爲它是,你永遠不會初始化thread_data,這就是爲什麼thread_data->thin_fn將是無效的,因此段錯誤。

相關問題