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 to deal with one class(object) with several steps function which need to run in multiple threads.

how to deal with one class(object) with several steps function which need to run in multiple threads.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 302 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.
  • J Offline
    J Offline
    JohnnyZ
    wrote on last edited by
    #1

    Hi all,

    Hope I can make it clearly and someone can help me, thanks in advance.

    Now I have a class such as:
    class A: QObject{
    Q_OBJECT
    ...
    private:
    Object data1; //queue
    Object data2; //queue

    void step1(); //create data1
    void step2(); //use data1, create data2
    void step3(); //use data2

    signals:
    ...
    public slots:
    ...
    }

    What I want to do is create a A object and run those 3 steps to make a whole task, and those 3 steps need to be run in its own thread, so need 3 threads at all.

    I found that only QThread::create() (Qt 5.10+) can make function run in a thread, but I don't know how to do it in this case.

    Can anyone give me some suggestions?

    1 Reply Last reply
    0
    • mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #2

      I'm not sure I completely understand, but can you create a public function in A that calls your three step functions, and do something like this:

          a = new A();
          thread = new QThread;
          a->moveToThread(thread);
          thread->start();
      

      You'd define a slot in the A c'tor to invoke the public function mentioned above.

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

        Hi and welcome to devnet,

        Sounds like you should take a look at the Qt state machine framework to model your situation.

        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
        • M Offline
          M Offline
          MrShawn
          wrote on last edited by
          #4

          Check out QRunnable and QThreadpool.

          Setup a threadpool with 3 threads (or more) and make runnables with your tasks that need to be processed.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JohnnyZ
            wrote on last edited by
            #5

            Many thanks to all of you, @mzimmers @SGaist @MrShawn , I will try things you suggested ASAP.

            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