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. What will happen if a function was called in other threads

What will happen if a function was called in other threads

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 580 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.
  • MihanM Offline
    MihanM Offline
    Mihan
    wrote on last edited by
    #1

    Hi
    I want to know somthing as the title
    e.g.

    int main 
    {
        type *function = type::getinstance();  //is a singleton
        QThread thread = new qthread(this);
        function->moveToThread(thread);
        thread->start();
    ...
        auto something = function->get_something(); //will return something
    }
    

    singleton:

    template <class T>
    class CSingleton: public QObject
    {
        class CSingletonGarbo
        {
            public:
            ~CSingletonGarbo()
            {
                if(m_instance)
                {
                    delete m_instance;
                    m_instance = NULL;
                }
            }
        };
    protected:
        CSingleton(){}
    private:
        CSingleton(const CSingleton&){}
        CSingleton& operator=(const CSingleton&){}
        static T* m_instance;
        static QMutex m_Mutex;
    
    public:
        static T* GetInstance();
    
    };
    
    template<class T> QMutex CSingleton<T>::m_Mutex;
    template<class T> T* CSingleton<T>::m_instance = NULL;
    
    template<class T> T* CSingleton<T>::GetInstance()
    {
        static CSingletonGarbo garbo;
        if(NULL == m_instance)
        {
            QMutexLocker locker(&m_Mutex);
            if(NULL == m_instance)
            {
                T* ptmp = new T();
                m_instance = ptmp;
            }
        }
        return m_instance;
    }
    

    It seems to work but I'm not sure that there is memory leak or not.
    Could you answer that and give me some suggestions?

    Regards
    Mihan

    1 Reply Last reply
    0
    • I Offline
      I Offline
      isdsi
      wrote on last edited by
      #2

      Not sure if the QThread you created is shutting down. If it is terminated, connect the deletion of the function using moveToThread when the thread is terminated with connect as below.
      QObject :: connect (& thread, & QThread :: finished, function, & QObject :: deleteLater);

      Please refer to the following documents.
      https://doc.qt.io/qt-5/qthread.html

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Just as a note.
        Calling a QObject instance for "function" is a sure way to confuse others reading the code. :)
        Also,
        if you put the singleton in a thread, why do you need a singleton at all ?

        Could just be a normal QObject worker as normally since they cant be copied anyway and im not sure
        what benefit you could possibly get from it also being a singleton. ?

        1 Reply Last reply
        1
        • MihanM Offline
          MihanM Offline
          Mihan
          wrote on last edited by
          #4

          Thank @isdsi @mrjj

          The reason why I use singleton is that it's a class of control module. This class will send orders to module and receive data from module, these will spend some time, and I want to ensure that it's the sole source.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Mihan said in What will happen if a function was called in other threads:

            function->get_something()

            What does this function do? Since you call it from the main thread, it's executed in the main thread so your thread is superfluous.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            MihanM 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @Mihan said in What will happen if a function was called in other threads:

              function->get_something()

              What does this function do? Since you call it from the main thread, it's executed in the main thread so your thread is superfluous.

              MihanM Offline
              MihanM Offline
              Mihan
              wrote on last edited by Mihan
              #6

              Thanks @Christian-Ehrlicher
              Is only function->get_something() in the main thread? Would other functions in this instance run in the thread that the instance moved to?

              get_something() is an interface to get something like data. But the data will be made in this instance.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Mihan said in What will happen if a function was called in other threads:

                Would other functions in this instance run in the thread that the instance moved to?

                Why should they? Please take a look at https://doc.qt.io/qt-5/qthread.html#details and the 'see also' section there to start learning on how to use threads (and QThreads)

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                MihanM 1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  @Mihan said in What will happen if a function was called in other threads:

                  Would other functions in this instance run in the thread that the instance moved to?

                  Why should they? Please take a look at https://doc.qt.io/qt-5/qthread.html#details and the 'see also' section there to start learning on how to use threads (and QThreads)

                  MihanM Offline
                  MihanM Offline
                  Mihan
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher
                  Should I use QSharedData to achieve it?
                  One thread makes data and write it into QSharedData, the other thread can read data from QSharedData

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    It's not about sharing data here, it's about when is a function executed in which thread. When you want to execute a function in another thread then this object must live in the other thread (which is already the case in your example) and then trigger the execution e.g. with Signals/Slots to change the thread.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2

                    • Login

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