[英]how to properly pass a struct pointer having a function member in pthread_create in c++
I'm working on an image processing project, where my job is to speed up the processing of a thinning algorithm. 我正在做一个图像处理项目,我的工作是加速细化算法的处理。
This is the snippet of initial code:- 这是初始代码的片段:
#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)
}
}
I want to split the 我想分开
cols_to_set.push_back(col);
rows_to_set.push_back(row);
into 2 separate threads to see if it helps speed things up. 分成2个独立的线程,以查看它是否有助于加快速度。
So here's what I tried:- 所以这是我尝试的:
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.
对于程序本身,您永远不会初始化thread_data
,这就是为什么thread_data->thin_fn
将无效,从而导致段错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.