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);
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();
}
}
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();
}
}
To copy to clipboard, switch view to plain text mode
this is the header for the worker object:
Q_OBJECT
public:
Worker(int* core);
~Worker();
public slots:
void testworker();
signals:
void finished();
//method to send info from worker thread to textbrowser in gui
void update_textbrowser
(QString info
);
private:
int thread_id;
};
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;
};
To copy to clipboard, switch view to plain text mode
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
);
}
Worker::Worker( int *core) {
this->thread_id = *core;
qDebug () << "constructor " + QString::number(this->thread_id);
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks