1

I am trying to multithread my image recording application in order to optimize performances and prevent the GUI from freezing. I tried to create a vector of CaptureThread (my class which extends QThread) but it does not compile...

here is my code:

vector<CaptureThread> v_ct_Threads(i_SelectedCameras);

for(int i = 0; i < i_SelectedCameras; i++) {
    v_ct_Threads[i] = CaptureThread(i, qsb_Duration->value());
    v_ct_Threads[i].start();
}

for(int i = 0; i < i_SelectedCameras; i++) {
    v_ct_Threads[i].wait();
}

And the error:

use of deleted function ‘CaptureThread& CaptureThread::operator=(CaptureThread&&)’
v_ct_Threads[i] = CaptureThread(i, qsb_Duration->value());

I guess it's a stupid mistake but I am a beginner in C++ and Qt...

Rémi
  • 75
  • 1
  • 1
  • 9

1 Answers1

4

The copy constructor of the CaptureThread is deleted, probably because the QThread cannot be copied.

You couldput pointers of your CaptureThreads into the vector of threads.

vector<std::unique_ptr<CaptureThread>> v_ct_Threads(i_SelectedCameras);

for(int i = 0; i < i_SelectedCameras; i++) {
    v_ct_Threads[i] = std::make_unique<CaptureThread>(i, qsb_Duration->value());
    v_ct_Threads[i]->start();
}

for(int i = 0; i < i_SelectedCameras; i++) {
    v_ct_Threads[i]->wait();
}
Christian G
  • 943
  • 9
  • 16
  • I updated to C++ 14 and your solution seems to work. Thank you. – Rémi Dec 13 '17 at 16:08
  • 1
    @BlackSilver-55 `std::make_unique` is part of c++14. See https://stackoverflow.com/questions/7038357/make-unique-and-perfect-forwarding for more information about it, how to work around it's absence in c++11 and how to implement your own version. – François Andrieux Dec 13 '17 at 16:08
  • 1
    In C++11 you can use: `v_ct_Threads[i] = std::unique_ptr(new CaptureThread(i, qsb_Duration->value()));` – Mike van Dyke Dec 13 '17 at 16:10