Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I want to know how can I use a non-void parameters function with a thread in Visual C++ 2010.

I think that it's best to paste directly my code, and ask you of show me how this functions goes modify for obtain same result with threads. I have problems when pass variables...please, help me.

This is the code:

XML
#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;

  }


How can use threads for do this works??
Posted
Updated 20-May-13 23:51pm
v2

Pack your two arguments into a struct and then pass it as the void * parameter, for instance (assuming you are using the Windows API CreateThread function):
C++
// ----------- thread creation code ------------------------
DWORD WINAPI my_fun( LPVOID lpParam );
struct MyArg
{
  Mat * p_in;
  Mat * p_out;
};

//...
Mat in, out;
MyArg arg;
HANDLE thread_handle;
DWORD thread_id;

// initialize in (and out, if needed) here
arg.p_in = ∈
arg.p_out = &out;
thread_handle = CreateThread( NULL, 0, my_fun, (LPVOID) &arg, 0, &thread_id]);
// ...


C++
// ------------------- thread code ------------------------
DWORD WINAPI my_fun( LPVOID lpParam )
{
  if ( ! lpParam)
  {
   // handle error here
  }
  Mat * p_in, * p_out;
  p_in = (( MyArg *)(lpParam))->p_in;
  p_out = (( MyArg *)(lpParam))->p_out;
  // ... use p_in and p_out here
}
 
Share this answer
 
If you do not want to use "void" you must choose another datatype to return from your method/function.

"Void" represents the datatype that is returned. If it is void, then nothing is returned. If it specifies any other datatype, it must return that.
 
Share this answer
 
v2
Comments
Domus1919 15-May-13 12:46pm    
Perhaps I was not clear: I need to launch a function that doesn't have "arguments" void, like the one I saw in the example, but "Mat" arguments (OpenCV)...

BeginThread of Visual Studio 2010 don't accept that argument functions with non-void, how can I do this?
Richard C Bishop 15-May-13 12:49pm    
I am not sure what you mean. "void" and method/function arguements are not related. Do you mean "NULL" by chance?
Domus1919 15-May-13 12:52pm    
I have posted example of function that I have to use.

"void example (Mat& in, Mat& out)"

When I have to start this function with beginthread, I have difficulty because when I wrote

hth1 = HANDLE_beginthreadx (NULL, 0, example, in, out,CREATE_SUSPENDED)

there is a problem because arg list it's not NULL or void.
Solution #1 and #2 both give you the answer but perhaps it is not clear to you. The direct answer is that you can't pass 2 parameters to a thread and can't call a function returning void. What you need to do is write a short method that returns DWORD and takes void* (aka LPVOID) as parameter. Inside that method, you unpack the data into your two values and call your method (ie. "example(x1, x2)"). See the code in Solution #2. In the my_fun() API you want to call example(in, out).
 
Share this answer
 
Comments
Domus1919 21-May-13 5:11am    
These solution for me it's clear, but don't works.

I paste here my code, and ask you your modify...

#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;

}

how can I do it with threads???
shailesh91082 23-May-13 5:49am    
which part of code you want to put in thread ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900