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. QThread and pointers
QtWS25 Last Chance

QThread and pointers

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadpointer
12 Posts 3 Posters 5.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.
  • B Offline
    B Offline
    beecksche
    wrote on 26 Apr 2016, 08:40 last edited by
    #1

    Hi,
    i have a question about QThread and pointers. I move my worker object to a created QThread (https://forum.qt.io/topic/66408/qthread-in-main-function). In the worker object there a pointers to other objects. These objects are created in a slot (so they are in a separate thread). Some pointers are shared with other objects.

    I'm not sure in which thread the pointers live in and if this is allowed or can occure some errors.

    My code looks like this

    class WorkerObject: public QObject
    {
        Q_OBJECT
    
    public Q_SLOTS:
        void createMembers() // create objects in a other thread
        {
            m_myObject1 = new MyObject1();
            QObject::connect(this, &QObject::destroyed, m_myObject1, &Qbject::deleteLater);
    
            m_myObject2 = new MyObject2(m_myObject1);
            QObject::connect(this, &QObject::destroyed, m_myObject2, &Qbject::deleteLater);
    
            ...
        }
    
    private:
        MyObject1 *m_myObject1;
        MyObject2 *m_myObject2;
        ...
    };
    
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Apr 2016, 08:49 last edited by
      #2

      Is createMembers() called before or after you move the object to other thread?
      Do you use queued connection?
      Is it necessary to create these objects on the heap via new?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        beecksche
        wrote on 26 Apr 2016, 08:54 last edited by
        #3

        CreateMembers() is called after i moved the workobject to the thread.

        The connection type is set to Qt::AutoConnection.

        Yes, i need them in some other slots again.

        K 1 Reply Last reply 26 Apr 2016, 10:44
        0
        • J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 26 Apr 2016, 09:21 last edited by
          #4

          Then it should be OK.
          The slot is executed in the thread where the object resides, so the objects are created in the same thread.
          Regarding sharing the pointers with other objects: in which thread are those other objects?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            beecksche
            wrote on 26 Apr 2016, 09:25 last edited by beecksche
            #5

            The other objects are also created in the createMembers() slot.

            So i can use the shared pointers in the other objects safely, because the objects "live" in the same thread, right?

            1 Reply Last reply
            0
            • B beecksche
              26 Apr 2016, 08:54

              CreateMembers() is called after i moved the workobject to the thread.

              The connection type is set to Qt::AutoConnection.

              Yes, i need them in some other slots again.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 26 Apr 2016, 10:44 last edited by
              #6

              @beecksche said:

              CreateMembers() is called after i moved the workobject to the thread.

              It makes a whole lot of difference how you call it. Is it connected to a signal, do you call it explicitly?

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • B Offline
                B Offline
                beecksche
                wrote on 26 Apr 2016, 11:27 last edited by
                #7

                Sorry, i haven't told you yet. It's connected to a signal!

                K 1 Reply Last reply 26 Apr 2016, 11:30
                0
                • B beecksche
                  26 Apr 2016, 11:27

                  Sorry, i haven't told you yet. It's connected to a signal!

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 26 Apr 2016, 11:30 last edited by
                  #8

                  @beecksche
                  Then it's perfectly fine.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    beecksche
                    wrote on 26 Apr 2016, 11:32 last edited by
                    #9

                    Perfect, thanks a lot!

                    K 1 Reply Last reply 26 Apr 2016, 11:36
                    0
                    • B beecksche
                      26 Apr 2016, 11:32

                      Perfect, thanks a lot!

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 26 Apr 2016, 11:36 last edited by
                      #10

                      @beecksche
                      No worries.
                      Although,QScopedPointer might be appropriate here (looking at your code), you may want to check it out.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        beecksche
                        wrote on 26 Apr 2016, 11:47 last edited by beecksche
                        #11

                        The advatage of a QScopedPointer is that you don't have to care about the destruction of the object, right?
                        The object will be destroyed when the pointer is out of scope?

                        K 1 Reply Last reply 26 Apr 2016, 12:41
                        0
                        • B beecksche
                          26 Apr 2016, 11:47

                          The advatage of a QScopedPointer is that you don't have to care about the destruction of the object, right?
                          The object will be destroyed when the pointer is out of scope?

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 26 Apr 2016, 12:41 last edited by
                          #12

                          @beecksche said:

                          The advatage of a QScopedPointer is that you don't have to care about the destruction of the object, right?

                          Yes. It's a thin wrapper around the raw pointer and will delete the held reference when it goes out of scope. The idea is to use the stack based QScopedPointer object to manage the heap-allocated object it's referencing.

                          Kind regards.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0

                          1/12

                          26 Apr 2016, 08:40

                          • Login

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