PDA

View Full Version : Multithreading - basics, passing information to threads



Lumbricus
18th January 2016, 15:38
Hey guys,

Im making a gui with workerthreads to do the work. My gui mainwindow generates the threads, assigns my worker object, connects the thread and the worker events. Doing this i want to pass a var that stands for the unique thread id to split up the work between threads.



void MainWindow::on_pushButton_2_clicked()
{
for (int id=1;id<=2;id++)
{

int *thread_id_on_heap_p =new int(id);

QThread *thread = new QThread;

Worker *worker = new Worker(thread_id_on_heap_p);

worker->moveToThread(thread);

//connect events from worker<-> threads
connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(thread, SIGNAL(started()), worker, SLOT(testworker()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

thread->start();
}

}


this is the header for the worker object:



class Worker : public QObject {
Q_OBJECT

public:
Worker(int* core);
~Worker();

public slots:
void testworker();

signals:
void finished();
void error(QString err);
//method to send info from worker thread to textbrowser in gui
void update_textbrowser(QString info);

private:
int thread_id;
};


this is the constructor of the worker object where i copy the id to the object :



Worker::Worker( int *core) {

this->thread_id = *core;

qDebug () << "constructor " + QString::number(this->thread_id);
}



after this i just use this->thread_id in my other worker functions, it seems to be working.

So my question is this a save way to pass the thread id? Will when on_PushBotton2_clicked() ends the variable will still be available since i passed the adress of the var? or is that adressspace free to be written on by other code? am i creating a memory leak here because i dont delete the data the pointer "thread_id_on_heap_p" points to? Are there better approaches for this problem?

be gentle guys :)

anda_skoa
18th January 2016, 17:47
Why are you allocating the int on the heap?
So that you have to deal with memory handling because it is fun or to leak the memory?

Cheers,
_

Lumbricus
18th January 2016, 18:01
Thx for your answer, i am very new to c++ and qt and i dont know better, for instance i learned that there was a stack and a heap yesterday. To the problem. I was passing an int to the thread and then just using it like this in the constructor:



thread_id = core;


while running calculations in a worker the id would change, so i figured i was doing something wrong :( i've just been trying different approches and this is the one i came up with that worked. Would you care to point me in the right direction?

thx again!

anda_skoa
18th January 2016, 20:01
Thx for your answer, i am very new to c++ and qt and i dont know better, for instance i learned that there was a stack and a heap yesterday. To the problem. I was passing an int to the thread and then just using it like this in the constructor:



thread_id = core;


That looks much better.
The type of the "core" argument should be just "int",

Cheers,
_

d_stranz
18th January 2016, 20:41
while running calculations in a worker the id would change

Not the way either version of your code is written. In both cases, the "id" is a constant, assigned to the "thread_id" variable using the for loop index, and then passed into the Worker constructor and assigned to the member variable.

So if the Worker:: thread_id member variable value is changing, then something somewhere else in your code is changing it. Qt or the operating system aren't doing it.