2 Replies
      Latest reply on Dec 29, 2019 5:20 PM by tigress
      tigress Level 1 Level 1 (0 points)



        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.

        • Re: Deleted Function
          jonprescott Level 2 Level 2 (90 points)

          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

            • Re: Deleted Function
              tigress Level 1 Level 1 (0 points)

              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);


              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?