Results 1 to 5 of 5

Thread: [QThread][QQueue] - logic problem !

  1. #1
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question [QThread][QQueue] - logic problem !

    Hi everyone,

    Directly to the matter !,

    - i have an application that have a parent class and two child classes
    - i have a queue of QUrl's (retrived from a table)
    - according to the url scheme i call one of the two classes method (with the same name, overriden !)

    Qt Code:
    1. if(url.scheme() == ...){
    2. Parent* p = new ChildA;
    3. p.DoSomething(url);
    4. }
    5. else if (url.scheme() == ...){
    6. Parent* p = new ChildB;
    7. p.DoSomething(url);
    8. }
    To copy to clipboard, switch view to plain text mode 

    - at the end of the DoSomething(url) i delete the QUrl from the table,

    What i want to do : having a max of 10 thread (for example), to do 10 DoSomething(url) at the same time, and at the end of one DoSomething(url) i call another do something on another thread with a newer url while the queue is not empty !

    so how i can do that ?
    another question
    : i use a QSqlDatabase on DoSomething(), can i use the same connection as a global variable or i have to create a new connection on each thread ?

    Thanks in advance , have a good day

    PS : i use Qt4, qtcreator as an IDE on linux

  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: [QThread][QQueue] - logic problem !

    You could make ChildA and ChildB implement the QRunnable Interface and then use a QThreadPool with 10 threads.

    Or, if you prefer manual thread handling, share a QSemaphore instance between your threads, initialized to 10, and have each thread acquire() it before it runs the operation and release() it when it is done.

    No idea about the database thread safety, that might depend on the driver, etc.

    Cheers,
    _

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

    Abdelhadi (18th April 2014)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QThread][QQueue] - logic problem !

    From Qt doc : A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.

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

    Abdelhadi (18th April 2014)

  6. #4
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QThread][QQueue] - logic problem !

    Quote Originally Posted by anda_skoa View Post
    You could make ChildA and ChildB implement the QRunnable Interface and then use a QThreadPool with 10 threads.

    Or, if you prefer manual thread handling, share a QSemaphore instance between your threads, initialized to 10, and have each thread acquire() it before it runs the operation and release() it when it is done.

    No idea about the database thread safety, that might depend on the driver, etc.

    Cheers,
    _
    Thank's for your reply, i have questions about each method if you don't mind:

    1 - about QThreadPool, do i have to create 10 different instances for ChildA/ChildB ?!, can you give me a good practice for this method
    2 - about QSemaphore, how i manage my QQueue with it ?

    let me explain better what i want to do : i have a list of Qurl's and depending on url scheme i instantiate ChildA or ChildB to run an overloaded method that takes the QUrl as parameter (it's always the same method), example :

    Qt Code:
    1. QQueue<QUrl> queue;
    2. foreach (QUrl url, urlsList){
    3. queue.enqueue(url);
    4. }
    5. while (!queue.isEmpty()){
    6. QUrl url = queue.dequeue();
    7. if(url.scheme() == ...){
    8. Parent * p = new ChildA;
    9. p.DoSomething(url);
    10. }
    11. else if (url.scheme() == ...){
    12. Parent * p = new ChildB;
    13. p.DoSomething(url);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    that means that i have to wait DoSomething(url) finish to treat the next url, what i want is doing this 10 times at the same time (treating 10 url's at the same time whatever the scheme) !

    sorry for my stupid questions, i'm a new bee


    Quote Originally Posted by Lesiok View Post
    From Qt doc : A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.
    Thank's for your reply, do you mean by that the QSqlDatabse ?


    Thank's eveyone
    Cheers

  7. #5
    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: [QThread][QQueue] - logic problem !

    Quote Originally Posted by Abdelhadi View Post
    [B]1 - about QThreadPool, do i have to create 10 different instances for ChildA/ChildB ?!, can you give me a good practice for this method
    More or less.

    The idea of a thread pool is that you have tasks. You create an instance of classes with a certain interface, each instance handling a single task. Then you enqueue these task objects on a thread pool.
    The pool then has each of its thread process one task and pick a new one when they are done (or make them wait until new tasks are available).

    You code snippet looked a lot like that approach.

    Qt Code:
    1. class ChildATask : public ChildA, public QRunnable
    2. {
    3. public:
    4. ChildATask(const QUrl &url) : ChildA(), m_url(url) {}
    5.  
    6. void run() {
    7. DoSomething(m_url);
    8. }
    9.  
    10. private:
    11. QUrl m_url;
    12. };
    To copy to clipboard, switch view to plain text mode 
    Or comibine that into ChildA itself.

    Qt Code:
    1. while (!queue.isEmpty()){
    2. QUrl url = queue.dequeue();
    3. if(url.scheme() == ...){
    4. Parent * p = new ChildATask;
    5. threadPool.start(p);
    6. }
    7. else if (url.scheme() == ...){
    8. Parent * p = new ChildB;
    9. threadPool.start(p);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 


    Quote Originally Posted by Abdelhadi View Post
    2 - about QSemaphore, how i manage my QQueue with it ?
    The semaphore would be used to limit the amount of parallelism, not the access to the queue.

    Lets assume that each of your Child classes uses a separate thread and each thread would call release() on the semaphore before it exits.

    So your work distribution loop would do something like this:
    Qt Code:
    1. while (!queue.isEmpty()){
    2.  
    3. semaphore.acquire();
    4.  
    5. QUrl url = queue.dequeue();
    6. if(url.scheme() == ...){
    7. Parent * p = new ChildA;
    8. p.DoSomething(url);
    9. }
    10. else if (url.scheme() == ...){
    11. Parent * p = new ChildB;
    12. p.DoSomething(url);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    If the semaphore has been initialized with 10, then the loop can acquire() it then times before any thread needs to be done. After that it will wait in acquire() until at least one of the 10 already running threads is done.

    Cheers,
    _

Similar Threads

  1. qt quick using c++ for the logic
    By fearu in forum Qt Quick
    Replies: 9
    Last Post: 17th January 2013, 12:34
  2. Craching in QQueue
    By kiran p in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2011, 09:57
  3. QQueue vs STL queue
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 8th November 2007, 18:35
  4. QQueue:QList::QStack:QVector?
    By jamadagni in forum Qt Programming
    Replies: 2
    Last Post: 20th August 2007, 13:29
  5. QThread and QQueue
    By qball2k5 in forum Qt Programming
    Replies: 19
    Last Post: 15th August 2006, 17:32

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.