私はC言語では比較的新しく、pthreadを使用している間に小さな画像フィルタを作成しようとしています。 C++:pthread_createのセグメンテーションフォールト
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
using namespace std;
using namespace cv;
#define WIDTH 3
#define HEIGHT 4
#define NUM_THREADS 4
struct readThreadParams{
Mat img;
Mat out;
int yStart;
int xEnd;
int yEnd;
int xRad;
int yRad;
};
//Find average of all pixels in WXH area
uchar getAverage(Mat &img, Mat &out, const float x1, const float y1, const int xRad, const int yRad){
//x1, y1: Pixel position being checked. xRad, yRad: how many pixels are being checked in x and y, relative to starting point.
uchar blue;
uchar green;
uchar red;
Vec3b outColor;
for (int c = 0; c < xRad; c++){
for (int r = 0; r < yRad; r++){
Vec3b intensity = img.at<Vec3b>(r, c);
blue =+ intensity.val[0];
green =+ intensity.val[1];
red =+ intensity.val[2];
}
}
outColor[0] = (blue/(xRad*yRad*4));
outColor[1] = (green/(xRad*yRad*4));
outColor[2] = (red/(xRad*yRad*4));
for (int c = 0; c< xRad; c++){
for (int r = 0; r< yRad; r++)
out.at<Vec3b>(Point(c, r)) = outColor;
}
}
void* parallel_processing_task(void * param){
//This is what each thread should do:
struct readThreadParams *input = (struct readThreadParams*)param;
Mat img = input->img;
Mat out = input->out;
const float yStart = input->yStart;
const float xEnd = input->xEnd;
const float yEnd = input->yEnd;
const float xRad = input->xRad;
const float yRad = input->yRad;
for (int c = 0; c < xEnd; c + xRad){
for (int r=yStart; r < yEnd; r + yRad){
getAverage(img, out, c, r, xRad, yRad);
}
}
}
int main(int argc, char *argv[]){
//prepare variables
pthread_t threads[NUM_THREADS];
void* return_status;
struct readThreadParams input;
int t;
Mat img = imread("image.jpg", IMREAD_COLOR);
int ROWS = img.rows;
int COLS = img.cols;
Mat out(ROWS, COLS, CV_8UC3);
input.img = img;
input.out = out;
input.xEnd = COLS;
input.xRad = WIDTH;
input.yRad = HEIGHT;
double t2 = (double) getTickCount();
for (int r = 0; r<ROWS ; ceil(ROWS/NUM_THREADS)){
input.yStart = r;
input.yEnd = r + ceil(ROWS/NUM_THREADS);
pthread_create(&threads[t], NULL, parallel_processing_task, (void *)&input);
}
for(t=0; t<NUM_THREADS; t++){
pthread_join(threads[t], &return_status);
}
t2 = ((double) getTickCount() - t2)/getTickFrequency();
//print execution time
cout << "Execution time: " << t2 << " s" << endl;
//result image
imwrite("output.png", out);
return(0);
}
は私が犯人を見つけるために、GDBを使用してのように得ることができた:ポインタや参照で遊んでの数時間後に、それはコンパイラを通過するが、その後、私はセグメンテーションフォールトを取得し、コードは以下のとおりです。これまで、それはライン107上にある見つけ出すよう:この時点で
pthread_create(&threads[t], NULL, parallel_processing_task, (void *)&input);
、私は解決策を見つけるために、あらゆる場所に行く試みたが、私は次のことを試してみました:
- 私は構造体を定義した方法を変更し、それを作る受け取ったポインタは、私は後でそれが動作していないことがわかった。
-
(*void)
の追加や削除などの引数を渡す方法を変更すると、 という大きなエラーが発生したり、最後に同じエラーが発生したりします。が#0__pthread_create_2_1(newthread=optimized out>, attr=<optimized out>, start_routine=<optimized out>, arg=<optimized out>) at pthread_create.c:601 #1 0x00011a00 in main(argc=1, argv=0x7efff394) at file.cpp:107
私の一部に問題が
optimized out
に関連していると思うしたい:GDBのBTの出力を読み取ろうとするとき
さらに、この言語に新しいものは本当に私を助けていませんそれを見ても結果は得られません。少なくとも、私は正しく見ていないかもしれません。
私はここで間違っているかもしれないと思っていますか?私は非常に助けていただければ幸いです!
「私は」作ることができ、任意の値を持つすることができるように、これは未定義の動作につながる可能性がある前に
でそれを使用して
t
を初期化していませんCで一般的に比較的新しくなっています」と、Cコンパイラではコンパイルせず、C++コンパイラではコードを投稿します。 – Stargateur'parallel_processing_task'は成功すると' nullptr'を返します。 – Galik