简体   繁体   English

在Windows OS中,如何将数据从OpenCV或C ++线程函数返回到主线程?

[英]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.

相关问题 如何在不使用C ++ / C的阻塞函数的情况下将值从线程返回到主函数 - How to return value from the thread back to main function without using blocking function in C++/C 从主循环访问线程变量-C ++-Windows - access to a thread variable from main loop - c++ - windows 在C ++ for Windows中挂起并恢复主线程 - Suspend and resume the main thread in C++ for Windows C++ 线程:如何使用 lambda function 将主线程中的参数传递给另一个线程 - C++ Thread: How to pass parameter in main thread to another thread with lambda function 在C ++中是否可以从主线程中执行辅助线程中运行的函数? - Is it possible in C++ to execute function in main thread from function that is running in secondary thread? 从另一个线程C / C ++对主线程执行类方法或函数(静态方法) - Execute class method or function(static method) on main thread from another thread C/C++ 如何在其他线程中将图像数据从BitmapSource(WPF)复制到C ++ / CLI中的cv :: Mat(OpenCV)? - How to copy the image data from a BitmapSource (WPF) to a cv::Mat (OpenCV) in C++/CLI in a different thread? C ++如何使dll从主线程修改变量? - C++ How to make a dll modify a variable from main thread? 如何在 c++ 中将值从线程传递到主线程 - How to pass a value from a thread to main in c++ C ++:使用io.post和bind命令从boost线程在主线程中执行函数 - C++: executing function in main thread from boost thread with io.post and bind command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM