Unsolved Not allowing a function which is accessed by its object, to be accessed by the same object before it finishes its progress completely
-
Qt Geeeeeeks,
I know what I am asking is very simple and I am not looking for a trick to solve it. What I want is a general solution.
Here is the story:
I have a class(classy) in my project. This class has its own files(.h and .cpp).
I also have another cpp file; Let us call this mainWindow.cpp.
I've got only one instance of classy in my mainwindow file (classy *obj=new classy;) But I have used obj in several different functions of it.
Due to the fact that the mainWindow.cpp is multi-threaded, functions can run concurrently.
It means obj starts to run a function of itself within a function of mainWindow before it finishes the same function of itself in another function of mainwindow.Now the Question: Is it possible to not allow a function which is accessed by its object, to be accessed by the same object before it finishes its progress completely?
Limitations:
1.mainWindow must be kept multi-threaded.
2.We can not create a new instance of classy. -
I'm not sure if I understood correctly what you want to do but I think you're asking for a mutex -> http://doc.qt.io/qt-5/qmutex.html
-
hi
you mean call same function again, before last call is finished? -
@Wieland
Hey there,
I know it is not well explained but I could not explain it better than this.
I don't think mutex can help me as far as no classy is not multi-threaded. -
@Nouriemm
Hello, I'm not understanding the question. Could you explain what thread calls what function? Functions are in the static part of the binary (the text section) and have no knowledge of objects or threads. Two different functions can be called from different threads, notwithstanding they are methods of the same object. Also do bear in mind that Qt's GUI is non-reentrant, meaning that objects' methods are not only thread-unsafe, but instances share data as well (can't be safely pushed around different threads).Kind regards.
-
@mrjj
Not, exactly
I want to call a function, right after the last call has finished its process completely with the same object.
In conclusion, we have only one function and one object.
This object appears several time in a multithread-program and calls the same function every time.
Therefore it uses the function of its class which is already being used by itself and returns unexpected results.I hope that clears the topic a little bit.
Cheers all
-
@Nouriemm Ok, now I understood. You definitely want a mutex. You should read a bit about thread-safety and reentrancy. It's a bit complicated but very important.
-
@Nouriemm
Are you talking about this:class A { void someFunction() { QMutexLocker locker(&someFunctionLock); // ... Do other stuff // This part is safely executed from multiple threads, because access to it is serialized through the mutex. // BUT! If you access unprotected static/global variables here, there's no guarantee at all that this will work! } private: QMutex someFunctionLock; }
Now, you have to know that calling someFunction() from someFunction() (even if it's indirectly invoked) will cause a deadlock.
Kind regards.
-
@Wieland
Interestingly, I have used mutex and it did not help (maybe I did not use it correctly).
Now your words are vibrating something in my head and I am going to try it again.
Will update the topic ASAP.Cheers pal