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. How can I sync Qthreads ?
QtWS25 Last Chance

How can I sync Qthreads ?

Scheduled Pinned Locked Moved General and Desktop
qthreadc++syncronization
6 Posts 4 Posters 2.4k Views
  • 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.
  • S Offline
    S Offline
    SujaMM
    wrote on last edited by
    #1

    Hello,

    I'm working with Qthreads in my program, but I'm having a problem with the syncronization. I'm acessing an Api to save the data in the database, but it's not working beacuse the Threads are not sync.

    So I'll like to know hoe can I sync the Qthreads to make sure not more than one tread it's accesing the api at the same time.

    I try ussing Qmutex and Qwaitcodition but it didn't work.

    Thanks

    M 1 Reply Last reply
    0
    • S SujaMM

      Hello,

      I'm working with Qthreads in my program, but I'm having a problem with the syncronization. I'm acessing an Api to save the data in the database, but it's not working beacuse the Threads are not sync.

      So I'll like to know hoe can I sync the Qthreads to make sure not more than one tread it's accesing the api at the same time.

      I try ussing Qmutex and Qwaitcodition but it didn't work.

      Thanks

      M Offline
      M Offline
      mellow
      wrote on last edited by
      #2

      @SujaMM

      QMutex is how I would do it. What do you mean didn't work?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SujaMM
        wrote on last edited by SujaMM
        #3

        I use QMutex to lock and unlock everytime I need to send a request to the Api. Like this:

        void Task::log(){
              sync.lock();
              currentapi->createLog(currentapi->jsonUser["id"].toInt(),currentapi->jsonSession["id"].toInt(),QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
              sync.unlock();
        }
        
        void Task::clog(){
             sync.lock();
             currentapi->closeLog(currentapi->jsonUser["id"].toInt(),currentapi->jsonLog["id"].toInt(),capThread->url, capThread->url,QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
             sync.unlock();
        }
        
        void Task::activity(){
              sync.lock();
              currentapi->newActivity(currentapi->jsonUser["id"].toInt(), idlog,"Qt Creator", Mcount, Kcount, "task.cpp - PerfQ_Client - Qt Creator",QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
              sync.unlock();
        }
        

        The three methods are slots, and the signals to this slots come from diferents threads.
        sync is the Qmutex.

        But it doesn't work, the program starts fine but when clog it's call the program stops working.

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

          Hi,

          Did you check that you are returning from log at some point ? Are you maybe blocked in createLog ?

          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
          • S SujaMM

            I use QMutex to lock and unlock everytime I need to send a request to the Api. Like this:

            void Task::log(){
                  sync.lock();
                  currentapi->createLog(currentapi->jsonUser["id"].toInt(),currentapi->jsonSession["id"].toInt(),QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
                  sync.unlock();
            }
            
            void Task::clog(){
                 sync.lock();
                 currentapi->closeLog(currentapi->jsonUser["id"].toInt(),currentapi->jsonLog["id"].toInt(),capThread->url, capThread->url,QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
                 sync.unlock();
            }
            
            void Task::activity(){
                  sync.lock();
                  currentapi->newActivity(currentapi->jsonUser["id"].toInt(), idlog,"Qt Creator", Mcount, Kcount, "task.cpp - PerfQ_Client - Qt Creator",QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
                  sync.unlock();
            }
            

            The three methods are slots, and the signals to this slots come from diferents threads.
            sync is the Qmutex.

            But it doesn't work, the program starts fine but when clog it's call the program stops working.

            M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            @SujaMM said:

            The three methods are slots, and the signals to this slots come from diferents threads.

            If Task is executed in a separate thread and is the only one accessing to the log you don't need to protect with mutex because by default connect uses QueuedConnection to call slots across threads.

            BTW, if you want/need mutexes I suggest also to use QMutexLocker; it is useful to avoid deadlocks if one function called between lock/ulock raises exceptions

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SujaMM
              wrote on last edited by
              #6

              Task is the main thread, I have two other threads that will emit the signal, one to call the slot log and the other one to call clog. while the signal to call the slot activity it's in the main thread.

              The think it's that the log it's call every minute and the clog it's call also every minute, but a minute after log it's call. The problem it's that the activity it's call every 20 seconds, and if log o clog are call at the same time as the activity the api can't handle both request and one of the request it's end returning a null pointer.

              I don't know if this explain the problem better. I'll try with the QMutexLocker.

              Thanks

              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