Deleted Function

Hi,


I am experimenting with threads in C++.


My line of code is:


std::thread sampler_thread((Sampler()));


It says: Attempted to use a deleted function thread.

The following program compiles:


#include <thread>

void sampler() { }

struct Sampler {
  void operator()() { }
};

struct NotASampler { };

int main(int argc, const char* argv[])
{
  auto lsampler = []() { };

  std::thread t1(sampler);
  std::thread t2((Sampler()));
  std::thread t3(lsampler);
  //std::thread t4((NotASampler()));

  return 0;
}


Note that trying to create a thread with the "NotASampler" gives the same error you mentioned in your post. You can't create a thread with a non-functional struct/class. Note also the need for an extra pair of parentheses around the call to the functional "Sampler," which is needed because you are not passing a function pointer.


Compiled with --std=c++17

Ok. Is there anyway to pass messages from one thread to another. For example,

if I create a thread like this:

std::thread manual_thread(&ManualSampler::loop, &ms);

manual_thread.detach();

where ms is static ManualSampler ms;

can I later on call a function on that thread. LIke ms.print();

?

Would that execute on the thread?

Deleted Function
 
 
Q