Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5956129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:12:46+00:00 2026-05-22T18:12:46+00:00

Hello guys I’m learning Qt and I’ve reached QThread class. Having no experience in

  • 0

Hello guys I’m learning Qt and I’ve reached QThread class. Having no experience in multithreading I spent several hours studying semaphores, mutexes, critical sections and wait functions in Win32API. When I launched several threads there and the ++ or — a global variable without synchronization I got different results each time. Now I am trying to do the same with QThread but I am getting failed. Can you tell me what’s wrong? here is my code:

#include <QCoreApplication>
#include <QMutex>
#include <QSemaphore>
#include <QThread>
#include <cstdio>

static const int N = 2000000;

class Thread : public QThread {
public:
    Thread();
    void run();
private:
    static QMutex mutex;
};

QMutex Thread::mutex;
static int g_counter = 0;

int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
    Thread A, B, C;
    A.run();
    B.run();
    C.run();
    char c;
    scanf("%c", &c);
    printf("%d\n", g_counter);
    return app.exec();
}

Thread::Thread() {

}

void Thread::run() {
    //QMutexLocker lock(&mutex);
    for (int i = 0; i < N; ++i) {
        ++g_counter;
        --g_counter;
    }
}

I expected to see g_counter jumping up and down as three threads are changing it at the same time. My problem was that I used run() so it executed as a simple function instead of start() to launch it as a thread. Anyway thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:12:46+00:00Added an answer on May 22, 2026 at 6:12 pm

    I’m not sure what you are trying to do here, but you need to start the thread by calling start(). You also need to lock the mutex, otherwise what’s the point?

    #include <QCoreApplication>
    #include <QMutex>
    #include <QSemaphore>
    #include <QThread>
    #include <cstdio>
    
    static const int N = 2000000;
    
    class Thread : public QThread {
    public:
      Thread(int id);
      void run();
    private:
      int id_;
      static QMutex mutex;
    };
    
    QMutex Thread::mutex;
    static int g_counter = 0;
    
    int main(int argc, char *argv[]) {
      QCoreApplication app(argc, argv);
      Thread A(0), B(1), C(2);
      A.start();
      B.start();
      C.start();
      char c;
      scanf("%c", &c);
      printf("%d\n", g_counter);
      return app.exec();
    }
    
    Thread::Thread(int id) : id_(id){ }
    
    void Thread::run() {
    
      for (int i = 0; i < N; ++i) {
        mutex.lock();
        ++g_counter;
        printf("g_counter: %d  thread: %d\n", g_counter, id_);
        mutex.unlock();
    
        mutex.lock();
        --g_counter;
        printf("g_counter: %d  thread: %d\n", g_counter, id_);
        mutex.unlock();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys i am having big problems in zend framework i dont know how
Hello can anybody solve this please I'm creating the object in the action class
Hello guys I am working on silverlight and I have issue regarding on database
hello guys i need small help in understanding file system of android Now in
Hello guys! I have been trying to create a if/else statement code in jQuery
Hello guys I have 3 images and a select with 3 option. The option
Hello guys i have a little problem i get an error: File C:\Users\kokki\Desktop\gb1\main.py, line
Hello Guys: I have a scrollView in which I've populated some uibuttons horizontally. The
Hello guys, just want you to ask something about my code... I have 5
Hello Guys! See I have been creating a code powered with ajax but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.