Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. pause/contiune Workerfunc in Thread
Forum Updated to NodeBB v4.3 + New Features

pause/contiune Workerfunc in Thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 411 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Boris1980
    wrote on last edited by Boris1980
    #1

    Hello Guys,

    i have a thread which calls a method in a cycle intervall:

    void run()
    
    {
    	while (true)
    		{
    			_holdOnMutex.lock();
    			
    			if(!_holdOn)
    			{
    			doJob() ; /* my function which have to complete and has to blocks the thread_pause method */
    			}
    
    			_holdOnMutex.unlock();
    
    			QThread::msleep(20);
    		}
    }
    

    now i want to pause and continue this thread in a conistent way, an wrote two mehtods:

    QMutex _holdOnMutex;
    bool _holdOn;
    void pause_Thread()
    {
    	_holdOnMutex.lock();
    	_holdOn = true;
    	_holdOnMutex.unlock();
    }
    void continue_Thread()
    {
    	_holdOnMutex.lock();
    	_holdOn = false;
    	_holdOnMutex.unlock();
    }
    

    is this a good approch, or there a better solution for my case?

    Thank for your suggestions...:)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      QWaitCondition might be of interest.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • enjoysmathE Offline
        enjoysmathE Offline
        enjoysmath
        wrote on last edited by
        #3

        I don't know if this is any better. But here's my pauseable_thread.py:

        from PyQt5.QtCore import QThread, QMutex, QWaitCondition
        
        class PauseableThread(QThread):  #(QThread):
            def __init__(self):
                super().__init__()
                self._pauseMutex = QMutex()
                self._pauseCond = QWaitCondition()
                self._paused = False
            
            def paused(self):
                return self._paused
            
            def pause(self):
                if self.isRunning() and not self._paused:
                    self._pauseMutex.lock()
                    self._paused = True
                    self._pauseMutex.unlock()
                
            def resume(self):
                if self.isRunning() and self._paused:
                    self._pauseMutex.lock()
                    self._paused = False
                    self._pauseMutex.unlock()
                    self._pauseCond.wakeAll()
            
            def checkPause(self):
                self._pauseMutex.lock()
                if self._paused:
                    self._pauseCond.wait(self._pauseMutex)
                self._pauseMutex.unlock()
        

        You then inherit from this class and call self.checkPause() in your main thread loop or anywhere you deem appropriate.

        https://github.com/enjoysmath
        https://math.stackexchange.com/users/26327/exercisingmathematician

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Boris1980
          wrote on last edited by
          #4

          Thank for your replay, and thank for your QWaitCondition suggestion, but what i dont understand the mechansi in your pausable trhead class:

          When call the pause method ,the paused flag is true,

          the checkpause method rrun to the wait condion ,

          the pausemutex is locked

          how can the resume mothode execute, if the pausemutex is locked!?

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved