[Solved]Can't run multiple thread on Linux

  • 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;

    void MainWindow::thread1()
    QMutex mutex;
    QMutexLocker locker(&mutex);

    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);

  • 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:

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.