Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++ How to launch non void function with thread

I want to ask how can I use a non void function as function to run with thread.

I mean, a function such as this:

void example(Mat& in, Mat& out)

How I can use this function for a thread with beginthreadx?

Paste the code that I've to trasform in multithreads code:

#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdio.h>
#include <windows.h>
#include <process.h>

using namespace std;
using namespace cv;

//filling array
void acquisisci (Mat in[]){
in[0]=imread("C:/OPENCV/Test/imgtest/bird1.jpg",1);
in[1]=imread("C:/OPENCV/Test/imgtest/bird2.jpg",1);
in[2]=imread("C:/OPENCV/Test/imgtest/bird3.jpg",1);
in[3]=imread("C:/OPENCV/Test/imgtest/pig1.jpg",1);
in[4]=imread("C:/OPENCV/Test/imgtest/pig2.jpg",1);
in[5]=imread("C:/OPENCV/Test/imgtest/pig3.jpg",1);
   }

//grey function
void elabora (Mat& in, Mat& out){
    if (in.channels()==3){
        cvtColor(in,out,CV_BGR2GRAY); //passa al grigio
        }
  }
  //threshold function
  void sogliata(Mat& in, Mat& out){
threshold(in,out,128,255,THRESH_BINARY);//fa la soglia
 }

 //view
  void view (Mat& o){
imshow("Immagine",o);
   waitKey(600);
   }

  int main(){

Mat in[6],ou[6],out[6];

acquisisci(in); 

for (int i=0;i<=5;i++){
elabora(in[i],ou[i]); 
}

for (int i=0;i<=5;i++){
sogliata(ou[i],out[i]);
}   

for (int i=0;i<=5;i++){
view(out[i]); 
}
  return 0;

  }

can I do this with parallel threads??

like image 528
Domenico Avatar asked Dec 08 '25 10:12

Domenico


1 Answers

_beginthreadex requires a specific signature for a thread function.

 void(*thread_func)(void *);

To use a function with a different signature, you normally just create a "thunk" -- a small function that does nothing but call the function you really want called:

struct params {
    Mat &in;
    Mat &out;        
};

void thread_func(void *input) { 
    params *p = (params *)input;

    example(input->in, input->out);
}

You'll probably also need to include something like a Windows Event to signal when the data is ready in the output -- you don't want to try to read it before the function in the thread has had a chance to write the data:

struct params {
    Mat &in;
    Mat &out;        
    HANDLE event;

    params() : event(CreateEvent(NULL, 0, 0, NULL)) {}
    ~params() { CloseHandle(event); }
};

void thread_func(void *input) { 
    params *p = (params *)input;

    example(input->in, input->out);
    SetEvent(input->event);
}

Then the calling function with start thread_func, and when it needs the result, do something like a WaitForSingleObject or WaitForMultipleObjects on the event handle, so it can continue processing when it has the data it needs.

like image 186
Jerry Coffin Avatar answered Dec 10 '25 01:12

Jerry Coffin