Results 1 to 5 of 5

Thread: Multithreading - basics, passing information to threads

  1. #1
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Multithreading - basics, passing information to threads

    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.

    Qt Code:
    1. void MainWindow::on_pushButton_2_clicked()
    2. {
    3. for (int id=1;id<=2;id++)
    4. {
    5.  
    6. int *thread_id_on_heap_p =new int(id);
    7.  
    8. QThread *thread = new QThread;
    9.  
    10. Worker *worker = new Worker(thread_id_on_heap_p);
    11.  
    12. worker->moveToThread(thread);
    13.  
    14. //connect events from worker<-> threads
    15. connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
    16. connect(thread, SIGNAL(started()), worker, SLOT(testworker()));
    17. connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    18. connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    19. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    20.  
    21. thread->start();
    22. }
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    this is the header for the worker object:

    Qt Code:
    1. class Worker : public QObject {
    2. Q_OBJECT
    3.  
    4. public:
    5. Worker(int* core);
    6. ~Worker();
    7.  
    8. public slots:
    9. void testworker();
    10.  
    11. signals:
    12. void finished();
    13. void error(QString err);
    14. //method to send info from worker thread to textbrowser in gui
    15. void update_textbrowser(QString info);
    16.  
    17. private:
    18. int thread_id;
    19. };
    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 :

    Qt Code:
    1. Worker::Worker( int *core) {
    2.  
    3. this->thread_id = *core;
    4.  
    5. qDebug () << "constructor " + QString::number(this->thread_id);
    6. }
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading - basics, passing information to threads

    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,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Multithreading - basics, passing information to threads

    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:

    Qt Code:
    1. thread_id = core;
    To copy to clipboard, switch view to plain text mode 

    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!

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading - basics, passing information to threads

    Quote Originally Posted by Lumbricus View Post
    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:

    Qt Code:
    1. thread_id = core;
    To copy to clipboard, switch view to plain text mode 
    That looks much better.
    The type of the "core" argument should be just "int",

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Lumbricus (19th January 2016)

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multithreading - basics, passing information to threads

    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.

  7. The following user says thank you to d_stranz for this useful post:

    Lumbricus (19th January 2016)

Similar Threads

  1. Replies: 3
    Last Post: 22nd September 2011, 06:47
  2. The very basics
    By KingOfHeart in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2011, 20:43
  3. Replies: 5
    Last Post: 19th August 2008, 16:42
  4. Replies: 2
    Last Post: 21st February 2008, 23:35
  5. Basics
    By Gayathri in forum Newbie
    Replies: 2
    Last Post: 17th November 2006, 12:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.