Results 1 to 9 of 9

Thread: QThreadPool - how to remove all threads

  1. #1
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThreadPool - how to remove all threads

    I have a QThreadPool and I limit the max amount of thread with setMaxThreadCount.

    Everything works fine but the problem is that I'm not able to delete all the threads in the pool.
    THe Threads are just downloading HTML files, but I really don't know how to obtain the list of threads and what to do with them (terminate?).

    Python code is perfect... C++ is... ok... I can read it 8-)

    THank you

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThreadPool - how to remove all threads

    Why do you want to delete threads in the pool?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThreadPool - how to remove all threads

    Because someone hit the "STOP" button and that shoud mean: please don't go ahead running the 100 QRunnable object that are waiting in the pool.

    Take this example. I have a list with 100 urls. I iterate the list and with with urllib2 I read data for every single url. setMaxThreadCount=4 so there are 4 running threads and all the other element are waiting in the pool. I want to stop the execution of all these "waiting" thread (and if it's possible also the 4 running!)

    Thank you

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThreadPool - how to remove all threads

    Then just make the runnable check whether it was canceled when it starts running. Or don't use threads at all, you don't need them for networking. Or use a queue of tasks and static threads instead of using QtConcurrent.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThreadPool - how to remove all threads

    Maybe I'm becoming too old for programmin 8-) THis is an excerpt of my code:
    Qt Code:
    1. class ConvertThreadDirect(QtCore.QRunnable):
    2. def __init__(self, item, soft, itemnumber):
    3. self.emitter = QtCore.QObject()
    4. QtCore.QRunnable.__init__(self)
    5. self.item = item
    6. self.soft = soft
    7. self.itemnumber=itemnumber
    8. self.softname=self.soft.softname
    9. self.str=''
    10.  
    11. def run(self):
    12. print "inside threadDirect"
    13. self.emitter.emit(QtCore.SIGNAL("itemSetChecking(PyQt_PyObject)"),(self.itemnumber))
    14. self.str=self.read_page(self.soft.url, self.soft.regexp)
    15. qlist=QtCore.QStringList([str(self.itemnumber), self.str])
    16. self.emitter.emit(QtCore.SIGNAL("itemCheckedDirect(PyQt_PyObject)"),(self.itemnumber, self.str, self.soft))
    17.  
    18. def read_page(self, urlStr, tobefound):
    19. pass
    20.  
    21. class MainForm(QtGui.QMainWindow):
    22. def __init__(self, parent=None):
    23. self.threadPool=QtCore.QThreadPool(self)
    24. self.threadPool.setMaxThreadCount(4)
    25. [....]
    26. [....]
    27. def actStop(self):
    28. print "I have to stop all the thread not started yet!!!"
    29. def actRun(self):
    30. print("Run")
    31. tree=self.ui.treeWidget
    32. nitem=tree.topLevelItemCount()
    33. self.nitem=0
    34. self.pb.setRange(0, nitem)
    35.  
    36. for i in range(0,nitem):
    37. item=tree.topLevelItem(i)
    38. s=self.softDic[unicode(item.text(0))]
    39. ct = ConvertThreadDirect(item, s, i)
    40. QtCore.QObject.connect(ct.emitter,QtCore.SIGNAL('itemCheckedDirect(PyQt_PyObject)'),self.itemCheckedDirect)
    41. QtCore.QObject.connect(ct.emitter,QtCore.SIGNAL('itemSetChecking(PyQt_PyObject)'),self.itemSetChecking)
    42. self.threadPool.start(ct)
    43. print item.text(0)
    44. self.ui.statusbar.showMessage(self.tr("Ready"))
    To copy to clipboard, switch view to plain text mode 

    As you can see I'm iterating over a TreeWidget and all the items are passed to a QthreadPool
    You are thinking to add a check in the run method of my class ConvertThreadDirect ?
    But how can I set the variable inside the run method from the main thread?

    I'm confused.

    Thank you

  6. #6
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThreadPool - how to remove all threads

    No suggestion?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThreadPool - how to remove all threads

    Use a global variable or something... Or don't use QtConcurrent but rather a queue as I already suggested.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Aug 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThreadPool - how to remove all threads

    In real life, writing code that does multiple tasks in parallel introduces new problems and bugs rising from synchronization issues between the tasks. In this article I'll explore what Qt has to offer to developers writing concurrent applications, and consider the best mechanism to use for every situation. Although writing parallel applications can be a pain, Qt does a lot to ease that pain........

  9. #9
    Join Date
    Sep 2016
    Location
    California
    Posts
    1
    Qt products
    Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: QThreadPool - how to remove all threads

    Quote Originally Posted by james_koley View Post
    In real life, writing code that does multiple tasks in parallel introduces new problems and bugs rising from synchronization issues between the tasks. In this article I'll explore what Qt has to offer to developers writing concurrent applications, and consider the best mechanism to use for every situation. Although writing parallel applications can be a pain, Qt does a lot to ease that pain........
    Really appreciate your reply on this post. It’s hard to sort the good from the bad sometimes, but I think you’ve nailed it!

Similar Threads

  1. Use QRunnable without QThreadPool
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2011, 10:02
  2. Qt Threads vs Native Threads performance
    By StackOverflow in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2010, 12:14
  3. QEventLoop and QThreadPool Shutdown Problems
    By shawno in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2010, 18:49
  4. Threads in Qt
    By freekill in forum Qt Programming
    Replies: 4
    Last Post: 11th November 2009, 18:49
  5. QThreadPool and QRunnable
    By jimc1200 in forum Qt Programming
    Replies: 3
    Last Post: 6th May 2009, 10:43

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.