0
static bool breakThread= false;

void TestMeUsingMiltiThreading(unsigned int uParentID)
{
    std::cout << "Thread " << uParentID << " Invoked Current thread " << std::this_thread::get_id();
    using namespace std::literals::chrono_literals;
    while (!breakThread)
    {
        std::cout << "Thread " << std::this_thread::get_id() << " is working..." << std::endl;
        std::this_thread::sleep_for(1s);//Thread wait for 1s before contuning further
    }
}
void TestMeUsingMiltiThreading()
{
    std::cout << "Current thread id is=" << std::this_thread::get_id();
    using namespace std::literals::chrono_literals;
    while (!breakThread)
    {
        std::cout << "Thread "<< std::this_thread::get_id() <<" is working..."<< std::endl;
        std::this_thread::sleep_for(1s);//Thread wait for 1s before contuning further
    }
}

.
.
.
int main()
{
.
.
.
std::thread worder(TestMeUsingMiltiThreading); //error here
.
.
.
}

Error: E0289 no instance of constructor "std::thread::thread" matches the argument list

How to fix this, I want to run threads over overloaded functions

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
newbie
  • 25
  • 4

1 Answers1

2

You need to tell it which overload you want since it can't deduce which function you want a function pointer to. It must be able to form a pointer to the proper TestMeUsingMiltiThreading even before you supply it to std::thread. Compare it with doing this:

auto func_ptr = TestMeUsingMiltiThreading; // error

It can't know which one you want here either.

One way to solve it is to do a static_cast<void(*)()> which makes it pick the only overload for which that cast works:

std::thread worder(static_cast<void(*)()>(TestMeUsingMiltiThreading));

Or using an intermediate variable to do it:

auto func_ptr = static_cast<void(*)()>(TestMeUsingMiltiThreading); // now ok
// or
//void(*func_ptr)() = TestMeUsingMiltiThreading;
std::thread worder(func_ptr);

Similarly, if you'd like to use the other overload:

std::thread worder(static_cast<void(*)(unsigned)>(TestMeUsingMiltiThreading), 1u);

Sidenote: breakThread should be a std::atomic<bool> since you are not synchronizing the reads from it with the writes to it.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108