1

I am new in multithread programming in c++. Today I had read that there is no copy constructor of std::thread and std::promise, my question is stupid simple: why? Yes, I understand that in some cases we have data race but there is cases where we don't have data race, it's not clear for me absolutely. Can anybody explain it?

I tried to do like this, but of course it didn't work: std::thread t1; std::thread t2 = t1; But the std::move work nice. I approximately understood why it works so, but I want to detailed explain

  • 3
    what would it mean to copy a `std::thread` ? Dont think too complicated, its not so much about data races, but just about how much sense it makes to copy a thread (not so much). – 463035818_is_not_an_ai Apr 05 '23 at 21:35
  • 3
    What would said copy constructor do in the first place? – HolyBlackCat Apr 05 '23 at 21:35
  • @HolyBlackCat it said that this costructor was deleted – Chingizhan Bahovich Apr 05 '23 at 21:41
  • . @463035818_is_not_a_number for me it mean something like this: take the function with what work our thread and place it to t2 to or take the scope with which our thread t1 work and place to t2 to work with them too. If I have mistakes in grammar, sorry for my english – Chingizhan Bahovich Apr 05 '23 at 21:41
  • I know, I'm asking what it would do if it wasn't deleted. So you're saying you'd want that to duplicate the thread, so the two threads would resume from the same point? – HolyBlackCat Apr 05 '23 at 21:43
  • to start a second thread that runs the same function you need not copy the thread, you start a new one. Copying the current state of execution of the function in one thread to another `std::thread` isnt easily possible. – 463035818_is_not_an_ai Apr 05 '23 at 21:46
  • For example, we have function `int x() { return 0; }` and I have `std::thread t1(x);` and the statement `std::thread t2(t1)` <=> (in my imagine) `std::thread t2(x);` – Chingizhan Bahovich Apr 05 '23 at 21:46
  • Ok, it stand more clear, may be I really complicate it – Chingizhan Bahovich Apr 05 '23 at 21:48
  • 1
    Short answer: You can't `fork()` threads. – tadman Apr 05 '23 at 21:50
  • well, there is an important distinction between the real object, the thread, and the c++ object to represent it. They are not identical. Read eg here https://en.cppreference.com/w/cpp/thread/thread. Perhaps `std::thread` could be copyable, but it would be counter intuitive, because the underlying object cannot simply be copied, you have to create a new, or perhaps it would not be counterintuitive, the point is its just a matter of how `std::thread` is defined. Its not impossible to imagine a representation of threads, where a handle can be copied to create a new thread – 463035818_is_not_an_ai Apr 05 '23 at 21:51
  • how I understood the problem is that we cannot "copy" the current state of our thread, it doesn't logical, because thread - independent in process from other threads, of course we can share data for several threads, but threads cannot communicate with each other, am I right? – Chingizhan Bahovich Apr 05 '23 at 22:00
  • its not logical, its just how `std::thread` is defined. – 463035818_is_not_an_ai Apr 07 '23 at 20:36

0 Answers0