[英]How to return data from OpenCV or C++ Thread function to main thread in case of windows os?
I am not much familiar with openCV or C++ threading. 我对openCV或C ++线程不太熟悉。
The main problem is I am passing some data to the thread function, it does some processing and after that IT NEEDS TO RESEND THE PROCESSED DATA. 主要问题是我正在将一些数据传递给线程函数,它进行了一些处理,然后需要重新发送处理的数据。
In Win 32 or VC++ this return data we can post/postthread through a message to the main thread in case of UI Threads, but in case of worker threads there is no facility to return the data. 在Win 32或VC ++中,如果是UI线程,我们可以通过一条消息将消息发布/发布到主线程中,但对于辅助线程,则无法返回数据。
What about openCV or C++ threading case how to send the return data to the main thread? 在openCV或C ++线程情况下,如何将返回数据发送到主线程呢? Can you please give me idea how to do this. 能否请您告诉我如何执行此操作。
The code I am using is as like below 我正在使用的代码如下
// .h file
#define MAX_THREADS 3
#include "windows.h"
typedef struct MyData {
unsigned char* colorPixelData;
uint32* punTIFFImageData;
int ii;
int jj;
int hh;
int ww;
int nWidth;
int kk;
} MYDATA, *PMYDATA;
void MyThreadFunction(void *arg);
and 和
// .cpp file, Main thread
PMYDATA pDataArray[MAX_THREADS];
PMYDATA pReturnedDataArray[MAX_THREADS];
HANDLE hThreadArray[MAX_THREADS];
//some code
hThreadArray[count] = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE)MyThreadFunction,
(void *)&pDataArray[count], // argument to thread function
0,
NULL);
WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);
//called thread function
void MyThreadFunction(void *arg)
{
//NEED TO RETURN DATA FROM HERE TO MAIN THREAD pReturnedDataArray
}
Use global variables instead so other threads can read/write on them. 请改用全局变量,以便其他线程可以对其进行读/写。
By doing this you don't need to worry about a thread "returning something". 通过这样做,您不必担心线程“返回某些东西”。
You can use std::thread
that is supported in C++11 standard. 您可以使用C ++ 11标准支持的std::thread
。 Then you can pass your pDataArray[count]
and as many other variables as you want so you don't have to return anything to get your output. 然后,您可以根据需要传递pDataArray[count]
和其他许多变量,因此您无需返回任何内容即可获取输出。 You can get more informations about this class here: http://www.cplusplus.com/reference/thread/thread/ 您可以在此处获取有关此类的更多信息: http : //www.cplusplus.com/reference/thread/thread/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.