how use Multithread?

i did these function:
1
2
3
4
void GetPixelImage(HDC HDCOrigin, int PosX, int PosY, COLORREF &color)
{
    color=::GetPixel(HDCOrigin, PosX, PosY);
}

heres how i use it:
1
2
3
COLORREF color;
            std::thread thread_obj(GetPixelImage, HDCOrigin,PosY+800,PosX,color);
            thread_obj.join();

why i get these error?
"error: no matching function for call to 'std::thread::_Invoker<std::tuple<void (*)(HDC__*, int, int, long unsigned int&), HDC__*, int, int, long unsigned int> >::_M_invoke(std::thread::_Invoker<std::tuple<void (*)(HDC__*, int, int, long unsigned int&), HDC__*, int, int, long unsigned int> >::_Indices)'|"
Try std::ref(color) instead of just color. #include <functional> as well.

You can't pass references to the thread object like that. JLBorges once showed me a workaround. Here's an example with an int ref:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <thread>
#include <functional> // std::ref

void GetPixelImage(int copy, int& ref)
{
    ref = 42;
}

int main()
{
    int beans = 3;
    int my_int = 17;
    
    std::thread thread_obj(GetPixelImage, beans, std::ref(my_int));
    thread_obj.join();
    
    std::cout << my_int << '\n';
}


See also: https://stackoverflow.com/questions/34078208/passing-object-by-reference-to-stdthread-in-c11/
This is also the case with (the bind expression created by) std::bind
The arguments to bind are copied or moved, and are never passed by reference unless wrapped in std::ref or std::cref. https://en.cppreference.com/w/cpp/utility/functional/bind#Notes
Topic archived. No new replies allowed.