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. Problem with QtConcurrent
Forum Updated to NodeBB v4.3 + New Features

Problem with QtConcurrent

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 606 Views 3 Watching
  • 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.
  • M Offline
    M Offline
    MrRobot1
    wrote on last edited by
    #1

    Hi
    I have a following problem: I'm trying to do a two threaded desktop (gui) application with using QtConcurrent class, such that, let's say, I would have two QTextEdit, two QPushButton, and in every QTextEdit fields would be realized an ordinary loop (e.g. for) in this way, when I would have clicked, let's say, the first button (linked to the first QTextEdit field) then the loop is realized, as mentioned in brackets, in first QTextEdit; then after e.g. 3 seconds I'm clicking the second button, and the second loop is starting in second field (QTextEdit) in a fully independent mode (so that I would be able to see, it's really multithread process). The both loops would show the incremental increased numbers with "append()" function, so that it would be easy to see how this two thread system is just functioning.

    JonBJ A 2 Replies Last reply
    0
    • M MrRobot1

      Hi
      I have a following problem: I'm trying to do a two threaded desktop (gui) application with using QtConcurrent class, such that, let's say, I would have two QTextEdit, two QPushButton, and in every QTextEdit fields would be realized an ordinary loop (e.g. for) in this way, when I would have clicked, let's say, the first button (linked to the first QTextEdit field) then the loop is realized, as mentioned in brackets, in first QTextEdit; then after e.g. 3 seconds I'm clicking the second button, and the second loop is starting in second field (QTextEdit) in a fully independent mode (so that I would be able to see, it's really multithread process). The both loops would show the incremental increased numbers with "append()" function, so that it would be easy to see how this two thread system is just functioning.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @MrRobot1 said in Problem with QtConcurrent:

      I'm trying to do a two threaded desktop (gui) application

      I don't understand what you are saying in your question. But, if it is relevant, you must not access (or at minimum not update) Qt GUI objects in any thread other than the single, main, GUI one. You can send signals from other threads to the GUI thread asking it to update something on behalf of the other thread, but you must not directly update from any other thread.

      1 Reply Last reply
      4
      • M Offline
        M Offline
        MrRobot1
        wrote on last edited by
        #3

        Haven't I said it clearly? Sorry, maybe for my English, I'm not a native speaker. I meant I wanted just to do an application, which would be with two threads (on the basis of QtConcurrent). That's all. I've searched even all WEB, looking for some example codes, just something even similar to that what I want to do, and I have nothing found. Only general examples of QtConcurrent applying. For me it is not enough. Maybe I'm not so intelligent, but it does not help me. Could you write me some even schematic code how to do this? Let us assume we have the main class window, then I understood what you have written but not good enough to be able to do such an application.

        jsulmJ 1 Reply Last reply
        0
        • M MrRobot1

          Haven't I said it clearly? Sorry, maybe for my English, I'm not a native speaker. I meant I wanted just to do an application, which would be with two threads (on the basis of QtConcurrent). That's all. I've searched even all WEB, looking for some example codes, just something even similar to that what I want to do, and I have nothing found. Only general examples of QtConcurrent applying. For me it is not enough. Maybe I'm not so intelligent, but it does not help me. Could you write me some even schematic code how to do this? Let us assume we have the main class window, then I understood what you have written but not good enough to be able to do such an application.

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MrRobot1 I suggest you take a look at examples: https://doc.qt.io/qt-5/examples-threadandconcurrent.html

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

          M 1 Reply Last reply
          3
          • jsulmJ jsulm

            @MrRobot1 I suggest you take a look at examples: https://doc.qt.io/qt-5/examples-threadandconcurrent.html

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

            @jsulm Thanks for the link, but I've known these examples already earlier, and they are a bit too complicated as, however, for me. As I've written yet: maybe someone could just write a simple, general code for described problem. Again, I would want to make an application, that would be two threaded, i.e., precisely: an application with two loops, being 'activated' (those loops) by clicking two buttons, respectively. I really do not have completely any idea how to do it. I don't know if I am allowed to put here some codes from my attempts?

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

              I seems like QtConcurrent is the wrong tool for the task. QtConcurrent is meant for single tasks that execute and then finish. You should also note that QtConcurrent uses a thread pool. Depending on the number of cores you have something might not execute with QtConcurrent immediately if other 'threads' (or rather 'tasks') are still using the pool.

              I would suggest using QThread for what you are trying to do. This has its own pitfalls but it is the better approach if you want to have an explicit thread (and not a task in a thread pool). Instead of a normal loop I suggest having a look into calling start() on the QThread to start the event loop for that thread. Use a QTimer with a timeout of 0s to successively call the same slot over and over again to simulate a loop (keep the counter somewhere else). In this way you can call quit() on the thread from the outside to stop it.

              M 1 Reply Last reply
              2
              • M MrRobot1

                Hi
                I have a following problem: I'm trying to do a two threaded desktop (gui) application with using QtConcurrent class, such that, let's say, I would have two QTextEdit, two QPushButton, and in every QTextEdit fields would be realized an ordinary loop (e.g. for) in this way, when I would have clicked, let's say, the first button (linked to the first QTextEdit field) then the loop is realized, as mentioned in brackets, in first QTextEdit; then after e.g. 3 seconds I'm clicking the second button, and the second loop is starting in second field (QTextEdit) in a fully independent mode (so that I would be able to see, it's really multithread process). The both loops would show the incremental increased numbers with "append()" function, so that it would be easy to see how this two thread system is just functioning.

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                @MrRobot1
                Based on how I understood your requirement, you should not use threading at all. Instead, use timers.

                1 Reply Last reply
                0
                • S SimonSchroeder

                  I seems like QtConcurrent is the wrong tool for the task. QtConcurrent is meant for single tasks that execute and then finish. You should also note that QtConcurrent uses a thread pool. Depending on the number of cores you have something might not execute with QtConcurrent immediately if other 'threads' (or rather 'tasks') are still using the pool.

                  I would suggest using QThread for what you are trying to do. This has its own pitfalls but it is the better approach if you want to have an explicit thread (and not a task in a thread pool). Instead of a normal loop I suggest having a look into calling start() on the QThread to start the event loop for that thread. Use a QTimer with a timeout of 0s to successively call the same slot over and over again to simulate a loop (keep the counter somewhere else). In this way you can call quit() on the thread from the outside to stop it.

                  M Offline
                  M Offline
                  MrRobot1
                  wrote on last edited by
                  #8

                  @SimonSchroeder Thank you very much. What you said sheds some light on my problem. I thought a few times in this way, i.e., that perhaps I'm using just unsuitable tool (QtConrurrent) to my task. Now I realized that there must have been a problem, the more, especially, as you write "(...)something might not execute with QtConcurrent immediately if other 'threads' (or rather 'tasks') are still using the pool."
                  Well then, I will have to make me focused on QThread and QTimer classes. Again, I'm very grateful for your suggestions.

                  PS: I don't know how to mark my problem is solved, here on forum (To be more precise: it is not yet -- in the meaning I've not made my application yet, but in the meaning that QtConcurrent is not suitable for my purpose). Can you, or someone else, tell me how to mark this?

                  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