0

The original source is as below :

    for (auto &core : cores)
        core->Run();

Member "cores" has the structure like below :

    std::vector<std::unique_ptr<Core>> cores;

I tried to make the code above multi-threaded by changing the "for" loop :

    for(auto &core : cores){

        threads[i] = std::thread(core->Run, this);
        i++;

    }

    i = 0;
    for (auto &core : cores){

        threads[i].join();
        i++;

    }

I get the following error :

    error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, x86::Cpu* const)’
    threads[i] = std::thread(core->Run, this);

(P.S. core->Run() is implemented in x86::Cpu::Run(), core->Run() is a method in a class x86::Core::Run())

I know what I did was kind of stupid, but I'm fairly new to using threads, so I'm having a hard time finding the right way to use these functions or understanding them at all.

I'm trying to make each core->Run() function run in a separate thread, but I'm getting confused of how to use the std::thread when the function is inside a vector of a unique_ptr, and I'm finding it hard to reference the method in the right way

I think my situation is kind of unique, I could not find anyone asking a question similar to mine

염준혁
  • 21
  • 2
  • **TL;DR**: If you want to create a bunch of threads that execute `Run` on each `core`, the correct initializer (syntax and all) is `std::thread(&Core::Run, &core);`. You got the member wrong, as well as the object (it's unlikely to be `this`). – StoryTeller - Unslander Monica Feb 18 '18 at 08:47
  • I tried your solution, and I'm getting the following error: /usr/include/c++/4.8/functional:558:20: error: pointer to member type ‘void (x86::Core::)()’ incompatible with object type ‘std::unique_ptr – 염준혁 Feb 18 '18 at 08:53
  • I missed the `unique_ptr`. Then it should be `core.get()` – StoryTeller - Unslander Monica Feb 18 '18 at 08:53
  • If i delete & in front of "core" (std::thread(&Core:;Run, core)), i get /usr/include/c++/4.8/tuple:135:25: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = x86::Core; _Dp = std::default_delete]’ maybe I should have posted this error first instead... (I'm trying many different syntaxes, and none of them is working, that's why I just posted the simplest error) – 염준혁 Feb 18 '18 at 08:55
  • o okok I'll try that thanks – 염준혁 Feb 18 '18 at 08:55
  • I at least passed the compiler error, thanks a lot! So, to my understanding, the first parameter is the definition of the method, and second one is the instance of the thread that I'm trying to run. Except that, for unique pointers, you have to use get() to receive the instance for that particular pointer? I just wanna make sure I understand things correctly, I'm fairly new to these kind of functions... – 염준혁 Feb 18 '18 at 09:01
  • The first argument is a "pointer-to-member-function". Something that is a bit of a quirk of C++. The next argument must be a point or reference to the type that member belongs to. You should pick up a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you want to get the hang of it. – StoryTeller - Unslander Monica Feb 18 '18 at 09:03

0 Answers0