Click here to Skip to main content
15,867,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a code from the basler video capture program as below which i want to make it as a thread to run it parallel to my main program:
C++
for (uint32_t i = 0; i < c_countOfImagesToGrab && cameras.IsGrabbing(); ++i)
{
 cameras.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
 intptr_t cameraContextValue = ptrGrabResult->GetCameraContext();
#ifdef PYLON_WIN_BUILD
 Pylon::DisplayImage(cameraContextValue, ptrGrabResult);
#endif
 cout << "Camera " << cameraContextValue << ": " << 
 cameras[cameraContextValue].GetDeviceInfo().GetModelName() << endl;
 cout << "GrabSucceeded: " << ptrGrabResult->GrabSucceeded() << endl;
 cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
 cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
 const uint8_t* pImageBuffer = (uint8_t*)ptrGrabResult->GetBuffer();
 cout << "Gray value of first pixel: " << (uint32_t)pImageBuffer[0] << endl << endl;
}

I've made a thread as below and place it in the While loop of my main program:
C++
std::thread t1([&](uint32_t i) {
for (uint32_t i = 0; i < c_countOfImagesToGrab && cameras.IsGrabbing(); ++i)
 {
 cameras.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
 intptr_t cameraContextValue = ptrGrabResult->GetCameraContext();
#ifdef PYLON_WIN_BUILD
 Pylon::DisplayImage(cameraContextValue, ptrGrabResult);
#endif
 cout << "Camera " << cameraContextValue << ": " << 
 cameras[cameraContextValue].GetDeviceInfo().GetModelName() << endl;
 cout << "GrabSucceeded: " << ptrGrabResult->GrabSucceeded() << endl;
 cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
 cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
 const uint8_t* pImageBuffer = (uint8_t*)ptrGrabResult->GetBuffer();
 cout << "Gray value of first pixel: " << (uint32_t)pImageBuffer[0] << endl << endl;
}
 }, & ptrGrabResult);

But when compile the program i had 2 errors:
C2672:'std::invoke no matching overloaded function found'
C2893:'Failed to specialize function template 'unknown-type std::invoke(Callable &&,_Types&&...)''


What I have tried:

I've trying 2 method to create this thread. Function pointer and lambda method but it remained the same result as 2 errors happened after each time compiling
Posted
Updated 27-Nov-19 0:19am
Comments
Member 14629414 27-Nov-19 5:42am    
Could anyone tell me how to solve this problem? I've stuck with this stuff about 1 week:((
I'm very grateful!!
Shao Voon Wong 27-Nov-19 6:10am    
What is the type of ptrGrabResult? Specify that type as the lambda parameter which is uint32_t at the moment. Give that parameter an unique and meaningful name, not i which is used in the lambda body.
Member 14629414 27-Nov-19 6:18am    
@Shao Voon Wong I even don't know exactly what parameter should pass to the thread
so i'm not sure ptrGrabResult is correct or not. But in the upper path of the code ptrGrabResult is defined as "CGrabResultPtr ptrGrabResult;"
It is the variable to stored the captured frame from camera.
Shao Voon Wong 27-Nov-19 6:24am    
See my answer below.
Member 14629414 27-Nov-19 6:32am    
@Shao Voon Wong Thank you so much the program can be compiled without any errors!!!
But it seem my main program don't work.Maybe because the main program has to wait
the thread until it close?.I want 2 program run parallel

1 solution

Remove the i parameter and &ptrGrabResult

C++
std::thread t1([&]() {
for (uint32_t i = 0; i < c_countOfImagesToGrab && cameras.IsGrabbing(); ++i)
{
    cameras.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
    intptr_t cameraContextValue = ptrGrabResult->GetCameraContext();
#ifdef PYLON_WIN_BUILD
        Pylon::DisplayImage(cameraContextValue, ptrGrabResult);
#endif
    cout << "Camera " << cameraContextValue << ": " << 
    cameras[cameraContextValue].GetDeviceInfo().GetModelName() << endl;
    cout << "GrabSucceeded: " << ptrGrabResult->GrabSucceeded() << endl;
    cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
    cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
    const uint8_t* pImageBuffer = (uint8_t*)ptrGrabResult->GetBuffer();
    cout << "Gray value of first pixel: " << (uint32_t)pImageBuffer[0] << endl << endl;
}
});

t1.join(); // have to call join() somewhere in the main thread.
 
Share this answer
 
v2

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