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. [Solved]Can't run multiple thread on Linux
Forum Updated to NodeBB v4.3 + New Features

[Solved]Can't run multiple thread on Linux

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.5k Views 1 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.
  • A Offline
    A Offline
    antonyprojr
    wrote on last edited by
    #1

    I'm trying to develop a program that run two thread for testing in Windows and Linux.
    One thread is locked by mutex and another thread increase +10 to int i.
    In Windows i get 10,20,30... , but in linux it always display 0.
    Apparently the thread are connected, and the second thread never can increase +10 to i.
    What is wrong with my code?

    @
    int i;
    QFuture<void>t1;
    QFuture<void>t2;

    void MainWindow::thread1()
    {
    QMutex mutex;
    QMutexLocker locker(&mutex);
    locker.mutex()->lock();
    }

    void MainWindow::thread2()
    {
    i += 10;
    }

    void MainWindow::on_pushButton_clicked()
    {
    if (!t1.isRunning()) t1 = QtConcurrent::run(this, &MainWindow::thread1);
    if (!t2.isRunning()) t2 = QtConcurrent::run(this, &MainWindow::thread2);
    QMessageBox *msgbox = new QMessageBox(this);
    msgbox->setText(QString::number(i));
    msgbox->exec();
    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexisdm
      wrote on last edited by
      #2

      If there is only one available thread in the thread pool, it won't execute the thread2 function.

      Maybe Qt didn't detect the number of cores/threads of your CPU (I assume it has more than 1 if it works on Windows). That number is returned by QThread::idealThreadCount, which, on linux, uses sysconf(_SC_NPROCESSORS_ONLN), which itself only returns the number of online processors as the name implies (I don't know if power saving affects the number returned by that function).

      Anyway, you can force a fixed number of threads with:
      @QThreadPool::globalInstance()->setMaxThreadCount(2);@

      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