0

I'm trying to make a class Threads to call for example 20 classes called Thread that will perform an action. Thread contains only function, this funtion is Compress and informations about the thread(if its busy etc.). I also have to add that Threads keeps Thread classes in std::vector called threads. Yeah i should have picked better names. haha

Whole program seems to work well, but when i use std::thread in Threads file to call Compress function from Thread class then I run into programs when i'm trying to compile this.

so first i will show code, but only parts that will be helpful, I think content of Thread.cpp will be useless, but I will post it here if you it will be needed

//Thread.h:

class Thread
{
public:
    BOOL isBusy = FALSE;

    Threads *threadParent;

    Thread();

    ~Thread();

    BOOL Compress() { return prvCompress(); }

private:
    BOOL prvCompress();
};

//Threads.h

class Threads
{
public:
    std::vector<Thread>threads;

    File *myFile;

    Threads(File *myFile_);

    ~Threads();

    BOOL CallCompress() { return prvCallCompress(); }

private:

    BOOL prvCallCompress();
};

Here from Threads.cpp i will just paste this calling function

//Threads.cpp

BOOL Threads::prvCallCompress()
{
    //for (UIN8 i = 0; i < ThreadsNumber; i++)
    //  std::thread(threads[i].Compress()).detach();


    std::thread(this->threads.at(0).Compress()).detach();

    return FALSE;
}

Errors:

Error number 1:
Severity    Code    Description Project File    Line    Suppression State
Error   C2893   Failed to specialize function template 'unknown-type std::invoke(_Callable &&) noexcept(<expr>)'    FileSlapper ...\VisualStudio\VC\Tools\MSVC\14.32.31326\include\thread   51  

Error number 2:
Severity    Code    Description Project File    Line    Suppression State
Error   C2672   'invoke': no matching overloaded function found FileSlapper ...\VisualStudio\VC\Tools\MSVC\14.32.31326\include\thread   55  

What have I tried:

So basically I tried doing this without std::thread and it works well.

I also tried to use this instead of calling that function:

    std::thread([&] { std::cout << threads[0].wasntUsed << std::endl; }).detach();

and this returned FALSE as it should, no errors

I also tried to just make a std::thread without even calling it, but it also ends up with an error:

    std::thread(this->threads.at(0).Compress());

if i forgot to add something I will definitely do it if you guys ask. But i'm sure here I posted everything that its needed.

Thank you guys in advance.

  • Unfortunately this was closed before I could post my answer; if you look at how you are spawning your thread, you are effectively doing this: `auto result = this->threads.at(0).Compress(); std::thread(result);`. However, `std::thread` needs a function to run, so instead you should provide it that expression as a function, e.g. `std::thread([&] { this->threads.at(0).Compress(); });` – mitch_ Jul 10 '22 at 10:28
  • 1
    You need to pass a callable object (a function-pointer, lambda, functor object, etc.) to the thread constructor. In your code you *call* the `Compress` function, which returns a `BOOL` value which I assume is an integer type, and as such it's not callable. – Some programmer dude Jul 10 '22 at 10:29
  • You need to pass a function pointer as first parameter of the `std::thread` constructor and a reference wrapper or pointer for the object to call the function for as a second parameter, if it's a member function pointer, followed by any function parameters: `std::thread(&Thread::Compress, std::ref(this->threads.at(0)))....` Note that you need to make sure the address of the `Thread` remains stable while the thread is running; adding elements to the `threads` vector could violate this requirement resuling in UB. – fabian Jul 10 '22 at 10:31
  • Simple solution: Use a lambda that calls the `Compress` function: `std::thread([&]() { threads.at(0).Compress(); }).detach();` – Some programmer dude Jul 10 '22 at 10:31

0 Answers0