Results 1 to 5 of 5

Thread: a design problem in multithread programming

  1. #1
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    2

    Unhappy a design problem in multithread programming

    Hi, I am using Qt4.5 to write a render program. Since the rendering is time-consuming, I create a thread to do it. Now my program's structure is like this. The generated pixels is store in a vector, and there is a QTimer in the rendering thread, so after a few millionseconds,the rendering thread can pass the newly generated pixels through a Signal. But when I run the programm,there always throw some memory excpetions, e.g Heap corruption.
    I think I should do a mutex lock somewhere,but after some experiments, the problem always existing.
    I think my design is poor, is there any other design can do this job in Qt?
    (Sorry for my poor English.)

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: a design problem in multithread programming

    It looks like producer-consumer problem (http://en.wikipedia.org/wiki/Producer-consumer_problem).
    You need to get access to the vector for both threads and lock them with mutex on write/read operations. Also if you have something like shared index number (first free slot in vector) you need to lock it before reading it and locking vector and unlock after updating it and unlocking vector. Then you can use signal/slot to notify the consumer thread that new data arrived to vector.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. The following user says thank you to faldzip for this useful post:

    ryen (4th April 2009)

  4. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a design problem in multithread programming

    would you mind sharing the relevant code?

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

    ryen (4th April 2009)

  6. #4
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    2

    Red face Re: a design problem in multithread programming

    Thanks for your attention! I've read the Mandelbrot example which is just fit my problem. And the rendering thread pass a QImage object instead of a vector to the GUI thread. I followed this and solved my problem. It seems that it is more difficult to manage the vector.
    Now I have a little problem. below is my rendering thread's run method.
    void MyThread::run()
    {
    render(); // is this unblock?
    exec();
    }
    Because I have a QTimer,so I need to add the "exec()" at the bottom. but if the "render()" fuction is executed in block-mode. How can the rendering thread start its own event-loop when the render fuction is being executed? (But now ,it just works, the readering thread can receive the Timer's timeout signals,and then pass the QImage to the GUI thread!)

  7. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a design problem in multithread programming

    everywhere in Qt where u see an exec() being used, its actually of the class QEventLoop..all classes, be it QDialog, QThread or QCoreApplication uses the exec of QEventloop internally..now what exec does is it ONLY processes the events that are posted/sent to the ur thread/main thread/dialog..so in ur render(), certain events are posted and they are executed when exec is called..event loop is started when u created the thread itself..when exec is called, those events are processed.

Similar Threads

  1. Problem with MultiThread..
    By blm in forum Qt Programming
    Replies: 6
    Last Post: 15th September 2008, 10:48
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. model View programming problem
    By mismael85 in forum Qt Programming
    Replies: 3
    Last Post: 2nd April 2008, 21:44
  5. Problem about timer in multithread programs
    By vql in forum General Programming
    Replies: 4
    Last Post: 17th October 2007, 15:00

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.