5

From multiple threads the following append function is called. I don't want data to re-write an append because the counter had not yet been incremented.

Will this suspend all threads coming in except for the one currently using Append? Or will the other threads just continue running and not append the data?

Does the mutex need to be "STATIC" or will each instance know to suspend operations?

If I don't want hiccups, I assume I have to build a buffer to back log data?

void classA::Append(int _msg)
{
    static int c = 0;
    QMutex mutex; //need to be static so other threads know to suspend?
                  //there are 10 threads creating an instantiation of classA or an object of classA     

    mutex.lock();

    intArray[c] = _msg;
    c++;

    mutex.unlock();
}
jdl
  • 6,151
  • 19
  • 83
  • 132

5 Answers5

4

No it doesn't need to be static, just make it a member in your classA and also you can take a look at QMutexLocker to scope lock and unlock the mutex:

void classA::Append(int _msg)
{
    static int c = 0;
    QMutexLocker locker(&mutex); // mutex is a QMutex member in your class

    intArray[c] = _msg;
    c++;

    /*mutex.unlock(); this unlock is not needed anymore, because QMutexLocker unlocks the mutex when the locker scope ends, this very useful especially if you have conditional and return statements in your function*/
}
Zlatomir
  • 6,964
  • 3
  • 26
  • 32
  • Btw, seems that int c; should be declared as class member too. Except of some "magic" code. – Dmitry Sazonov Jul 24 '13 at 15:59
  • c need to increase at each call. One thread instantiation will not know what the other thread did to "c" if it is not static. There are 10 instantions of this class. – jdl Jul 24 '13 at 16:04
1

The QMutex does not need to be declared as static and Qt will ensure that other threads will wait until the unlock occurs on the mutex before allowing another thread to continue execution in that function.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
0

To solve my problem, after multiple runs, I did have to make mutex "Static" due to multiple instantiations of classA. Making mutex a member did not work.

void classA::Append(int _msg)
{
    static int c = 0;
    static QMutex mutex; //YES... need to be static so other threads know to suspend
                         //there are 10 threads creating an instantiation of classA or an object of classA     

    mutex.lock();

    intArray[c] = _msg;
    c++;

    mutex.unlock();
}
jdl
  • 6,151
  • 19
  • 83
  • 132
0

@jdl "Making mutex a member did not work". Yes it works. Try to make mutex "Static" like this:

//classA.h
class ClassA {
    public:
        static QMutex mutex;
        // rest of variables and Methods
        // ...
}


//classA.cpp
QMutex ClassA::mutex; // Before all
ClassA::ClassA() {
    //Constructor
}
void ClassA::Append(int _msg) {
    static int c = 0
    QMutexLocker locker(&mutex)
    intArray[c] = _msg;
    c++;
}
-5

Since I don't know how QMutex is really operating, I just did my own mutex.

void classA::Append(int _msg)
{
    static int c = 0;
    static int mutex = 0; //YES... need to be static so other threads know to suspend
                     //there are 10 threads creating an instantiation of classA or an object of classA     

    while(mutex == 1){
        //suspend thread
    }

    if(mutex == 0){
        mutex = 1;//lock        

        intArray[c] = _msg;
        c++;

        mutex = 0;//unlock
    }

}
jdl
  • 6,151
  • 19
  • 83
  • 132
  • 2
    This is actually not a real mutex lock. This is because two threads could see that mutex is zero and go into the if statement at the same time. It's actually probably more likely Ruth the while mutex equals 1 part above. – jnesselr Apr 12 '14 at 16:53