Wait until condition is not met - but why mutex?
-
Hi,
Because you want to access a shared resource and thus its access must be protected.
-
@SGaist But I know I use only once variable "a" in second Thread.
EDIT
I agree with you. My example is very uncommon. But why we can't have something like:
mainThread: another operations waitCon.wait(); mutex.lock(); another important operations mutex.unlock(); another operations second Thread: another operations mutex.lock(); a = 6; mutex.unlock(); waitCon.wakeAll(); another operations
And function wait() can be without parameter "QMutex *lockedMutex".
-
In that case it's simply used as a mean to stop the thread execution until the wait condition is awaken.
Depending on what you want to implement, using signals and slot might be a better tool.
-
Because there are none.
Can you explain what your two threads are doing ? Especially why the first one needs to wait for a to be equal to 6 ?
-
@SGaist I don't have at the moment very good example Sometimes I have code:
fun() { fun1(); fun2(); fun3(); }
void timerTimeoutSlot() { do Something // for example set very important Variable; }
I would like to something like:
fun() { fun1(); wait when the very important variable will be set fun2(); fun3(); }
Because when this variable will not set my fun2() can't be execute.
-
And fun should be called repeatedly ?
-
@SGaist said in Wait until condition is not met - but why mutex?:
repeatedly
When I look at my code I think I can do something like:
void timerTimeoutSlot() { do Something // for example set very important Variable; fun2(); fun3(); }
But Could you tell me what is going on when signal is emitted? I talk about only 1 thread. For example:
function() { operation a; operation b; operation c; operation d; operation e; operation f; }
timerTimeoutSlot() { operation m; operation n; operation o; }
timer2TimeoutSlot() { operation x; operation y; operation z; }
And when the operation c is execute, signal timeout in timer1 is emitted
So operations will be execute like:
a -> b -> c -> m -> n -> o -> d -> e -> f?And second question:
When the operation c is execute, signal timeout in timer1 is emitted and when operation m is execute, signal timeout in timer 2 is emitted. So operation will be execute like:
a -> b -> c -> m -> x->y->z->n -> o -> d -> e -> f?EDIT:
I hear that can be something like:
a -> b -> c -> m -> d->n->e->o->f ( in first question ) -
If single threaded, the slots will be called after the functions are done in order.
Looks like you are trying to model some sort of behaviour. What would that be ?
-
Your answers makes it look like you are trying to model some behaviour that could be represented by a state machine rather that complex threading.
Since you are starting in the programming field, you should rather stay away from the threading paradigm and learn the rest first. Threading is a very complex topic that offers you many way to shoot yourself in the foot from different unrelated angles at different unrelated time with different unrelated side-effects that will want to go after your foot again.
-
If timer2 timeout value is greater than timer they will be in order: function -> timerTimeoutSlot -> timer2TimeoutSlot.