1

I would like to launch a member function in a separate thread calling it from another member. Maybe the code below is clearer.

There is a button which launches the counter in a thread and it works:

void MainWindow::on_pushButton_CountNoArgs_clicked()
{
   myCounter *counter = new myCounter;
   QFuture<void> future = QtConcurrent::run(counter, &myCounter::countUpToThousand);
}

MyCounter class member functions:

void myCounter::countUpToHundred()
{
   for(int i = 0; i<=100; i++)
   {
      qDebug() << "up to 100: " << i;
   }
}


void myCounter::countUpToThousand()
{
   for(int i = 0; i<=1000; i++)
   {
      qDebug() << "up to 1000: " << i;
      if (i == 500)
      {
          //here I want to launch myCounter::countUpToHundred() in another thread
      }
   }
}

Thanks in advance.

Jan
  • 2,060
  • 2
  • 29
  • 34
iGian
  • 11,023
  • 3
  • 21
  • 36
  • 3
    What is your problem? You've already used `QtConcurrent::run` for a member function in a button click slot. – hank Oct 08 '15 at 09:20
  • Well, when the counter reaches 500, the function must call a member function of the same class, it is something like a nested call. Maybe I must declare a nested object `myCounter *counter = new myCounter` ? – iGian Oct 08 '15 at 10:28
  • `this` pointer will help you: `QtConcurrent::run(this, &myCounter::countUpToHundred)` – hank Oct 08 '15 at 13:05

1 Answers1

1

Assuming you want to run the 2 counters parallel, you have 3 threads:

Thread 1: UI-Thread (or main thread)

Here runs on_pushButton_CountNoArgs_clicked(). You should not do hard work in this function because if you want to achive 60 frames per second, you only have 16 ms for all the work. To starting a new thread to run countUpToThousand() is a good idea.

Thread 2: background thread (started by QtConcurrent, running countUpToThousand)

This runs in parallel to Thread 1, and you are working with the same instance of myCounter (i.e. the same place in memory) so be careful which member variables you read and write.

Thread 3: background thread (started by QtConcurrent, running countUpToHundred)

Start using (as hank pointed out)

void myCounter::countUpToThousand()
{
   for(int i = 0; i<=1000; i++)
   {
      qDebug() << "up to 1000: " << i;
      if (i == 500)
      {
          QtConcurrent::run(this, &myCounter::countUpToHundred);
      }
   }
}

This will run in parallel to Thread 1 and Thread 2.

Now you might get crazy output results like 988\n99\n when one counter is at 999 and the other is at 88 because Thread 2 and Thread 3 will be printing to console at the same time and don't care about what the other thread is doing.

Also note that you must not delete counter before Thread 2 and Thread 3 are done because of you do, the'll still try to access the memory and your application will probably crash.

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • Well, thaks a lot Simon, **it works!** I found that my nested solution worked but I did not like for creating a new instance of the object. – iGian Oct 09 '15 at 14:33