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. Threading...
QtWS25 Last Chance

Threading...

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.2k 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.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    I have a multi-threaded application with an object that has re-entrant functions. I am trying to make my object thread safe by including a mutex. I am wondering if you can do multiple QMutexLocker locker on one instance of mutex within the same thread. For instance:

    @
    class Sample : Public QThread
    {
    protected:
    void run();

    public:
    Sample();
    ~ Sample();

    private:
    mutable QMutex mutex;
    bool Connect()
    {
    QMutexLocker locker(&mutex);

    Disconnect();
    

    };
    bool Disconnect()
    {
    QMutexLocker locker(&mutex);

    obj.close();
    

    };
    bool ReadRegister();
    bool WriteRegister();
    }
    @

    notice in my connect function I am calling my Disconnect function that also tries to do a QMutexLocker on global variable mutex.

    • Tien
    1 Reply Last reply
    0
    • T Offline
      T Offline
      tbscope
      wrote on last edited by
      #2

      bq. I am wondering if you can do multiple QMutexLocker locker on one instance of mutex within the same thread.

      Yes. When the mutex is locked, all other functions that require it to be unlocked, will wait (block) till the mutex is unlocked.

      Edit: But in your case, you will halt the thread forever. You can not call a function that requires the mutex to be unlocked from within a function that actually locks the mutex unless you unlock the mutex first. But in your case, the mutex is unlocked when the connect() function goes out of scope (the mutex locker is destroyed)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phamtv
        wrote on last edited by
        #3

        is it true i can set the recursive mode for the mutex to recursive for me to continue my process? How can I set the recursive mode? I tried the following but get an error:

        @
        class test : Public QThread
        {
        protected:
        void run();

        private:
        QMutex mutex;
        public :
        test()
        {
        mutex = new QMutex(QMuted::Recursive); // i can´t construct mutex object...
        };
        ~test();

        }
        @

        1 Reply Last reply
        0
        • P Offline
          P Offline
          phamtv
          wrote on last edited by
          #4

          i got it to work.... QMutex has to be declare as a QMutex pointer for me to do a new....

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tbscope
            wrote on last edited by
            #5

            QMuted should be QMutex?

            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