Results 1 to 7 of 7

Thread: design pattern for long time operation

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default design pattern for long time operation

    Hello.
    I don't know if this is right place to write this, so pardon me if it's not.
    Problem: perform many operation (like 12k++) one after another (to shorten this time I want to execute each operation as soon as previouse is finished). So in nutshell the same set of functions are performed many times on different set of data.

    My implementation:
    Currently I do it "wrong way" and not so wrong (maybe a right?) way.

    Wrong way using signal/slot mechanism (wrong in terms of logical implementation, not in code itself) - pseudo code:

    Qt Code:
    1. void myProgrma::myProgrma(){
    2. connect( this, SIGNAL( jobDone() ), this, SLOT(controlSLOT()));
    3. myProgrma();
    4. }
    5. void myProgrma::controlSLOT()
    6. {
    7. if(jobIsDone)
    8. return;
    9. else
    10. doJobSLOT()
    11. }
    12.  
    13. void myProgrma::doJobSLOT()
    14. {
    15. //do job here
    16. emit jobDone();
    17. }
    To copy to clipboard, switch view to plain text mode 
    As you can see there is one flaw with this implementation with is that for many, like 64k++, jobs (or less depending on what is in doJobSLOT() ) this will fail (AFAIK - please elaborate about this, if my thinking is wrong) due to the exceeding job queue stack size - due to not returning from slots that's leads to queue being full and thus error.

    I found out, on my trails and errors path, that using only signal/slot mechanism is a loot more faster then using QTimer solution (QTimer is about 6+ times slower). I guess that's due to the all those checks and overhead of the QTimer class. Basically I'm looking for fastest i9mplementation of this problem.

    I know that I can implement solution for this problem using QTimer and timeout() signal, but my question is:
    Is there any other way to implement "while/for" loop behaviour (basically above example does those loops) (or AFAIK so called Observer Pattern) in Qt?

    Thank you for any advice on this mater.
    Best regards.

    PS. in pseudo code "jobIsDone" is bool to check if there is any pending job to be done.
    Last edited by Talei; 21st July 2011 at 23:44. Reason: updated contents
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: design pattern for long time operation

    Hi,

    in your pseudo-code the signal and slot connection behave like a normal while loop. So for what reason do you need that slot?

    It all sounds to me, that you should use a thread which performs the 12k++ tasks in a simple for-loop that just checks every time for a stop-condition. For communicating with your main program use signals and slots.

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

    Talei (22nd July 2011)

  4. #3
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: design pattern for long time operation

    Thank you for reply. Your solution is great, and to be honest I thought about it myself but, I didn't see "the whole picture".
    Basically my idea was to do some kind of Controller to manage 12k+ task and divide it for X thread (x = QThread::idealThreadCount() ) and my problem was in this X.
    But Who the hell change CPU during program run??? I mean I have thread num so I divide 12k+/idealThreadCount() and pas this "job list" into each thread. Problem solved.

    Again thanks for a help.

    PS. Why is above (modified below) code crash for 6k+ jobs?
    Modified code:

    Qt Code:
    1. void myProgrma::myProgrma(){
    2. connect( this, SIGNAL( jobDone() ), this, SLOT(controlSLOT()));
    3. myProgrma();
    4. int count = 12000;
    5. }
    6. void myProgrma::controlSLOT()
    7. {
    8. if(count == 12000)
    9. return;
    10. else
    11. doJobSLOT()
    12. }
    13.  
    14. void myProgrma::doJobSLOT()
    15. {
    16. //do job here
    17. emit jobDone();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Talei; 22nd July 2011 at 00:11.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: design pattern for long time operation

    You can as well implement this without thread (it you wish to), just trigger the jobs on a QTimer with 0 ms timeout.

    Qt Code:
    1. void myProgram::myProgram(){
    2. QTimer * timer = new QTimer(this);
    3. connect(timer, SIGNAL(timeout()), this, SLOT(doJobSLOT()));
    4. timer->start();
    5. }
    6.  
    7. void myProgram::doJobSLOT()
    8. {
    9. //do job here
    10. if(!isJobsPending())
    11. timer->stop;
    12. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Santosh Reddy for this useful post:

    Talei (22nd July 2011)

  7. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: design pattern for long time operation

    One can't tell much because of the pseudo code...
    Is count global? In your example it isn't thus you have an infinite loop. Further do you increase count? And why do you call the c-tor inside the c-tor??? (line 3)

  8. The following user says thank you to Lykurg for this useful post:

    Talei (22nd July 2011)

  9. #6
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: design pattern for long time operation

    @Santosh Reddy Thanks for reply. I know, and already implemented, solution with QTimer and timeout() (works like a charm, except it's 6 time slower then signal/slot "solution", probably due all those timer checks), see my first post, but I'm looking, because of educational reason, for more possibilities to solve this problem, preferred without QThread.

    @Lykurg
    The code is irrelevant and I wanted only show the idea and the problem with signal/slot queue stacking.
    Yes "count" is global, I wanted just show type of it, and Line 3 is an error, should be controlSLOT();

    corrected Example:
    Qt Code:
    1. void myProgrma::myProgrma(){
    2. connect( this, SIGNAL( jobDone() ), this, SLOT(controlSLOT()));
    3. controlSLOT();
    4. count = 12000; //int count global
    5. }
    6. void myProgrma::controlSLOT()
    7. {
    8. if(count == 12000)
    9. return;
    10. else
    11. doJobSLOT()
    12. }
    13.  
    14. void myProgrma::doJobSLOT()
    15. {
    16. //do job here
    17. emit jobDone();
    18. }
    To copy to clipboard, switch view to plain text mode 

    I attached an example that shows the error of signal/slot and QTimer solution. QTimer is slower then signal/slot.
    Attached Files Attached Files
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  10. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: design pattern for long time operation

    Either use QTimer or use plain recursive calls.

    QTimer : all you jobs are guaranteed to work, job completion time may be large (here time is not limited)
    Recursion: only a limited number of jobs can be completed, as stack space is limited (here memory is limited)

    Using signals slots in recursive mode is very bad idea, you are basically are not using sigals / slots at all. Rather just call the slot directly like this.
    Qt Code:
    1. void MainWindow::doJob()
    2. {
    3. //do job
    4. jobCount++;
    5. // emit jobDone(); //with this jobs completed before stack is finished = 6166
    6. jobControler(); //with this jobs completed before stack is finished = 21584
    7. return;
    8. }
    To copy to clipboard, switch view to plain text mode 
    notice how much of the stack is being wasted by using signal / slot in recurssive mode, more over you can never easily detect a stack overflow condition, so your program may crash at any point.

    Recursion is good technique, but has to be used wisely for a controlled number of iterations.

    So for educational purpose, even if you move things to a separate thread or multiple threads, you will still run into same problems, as thread by themselves use a significant amount of memory compared to recursive call stack

  11. The following user says thank you to Santosh Reddy for this useful post:

    Talei (22nd July 2011)

Similar Threads

  1. Long operation -> Refresh user interface
    By pl01 in forum Qt Programming
    Replies: 2
    Last Post: 31st January 2011, 11:23
  2. design pattern for supporting multiple DB's?
    By scarleton in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2010, 15:33
  3. Design pattern of qwt library
    By Hogwarts in forum Qwt
    Replies: 1
    Last Post: 4th June 2010, 11:04
  4. GIS design pattern
    By olosie in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2009, 17:19
  5. Observer Design Pattern
    By joseph in forum General Programming
    Replies: 1
    Last Post: 21st January 2008, 13:17

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.