systhread_mutex_lock vs systhread_mutex_trylock

Timo Rozendal's icon

What is the best way of choosing between mutex_lock and mutex_trylock?
What does mutex_lock do when the mutex is already locked? does it wait?

I looked at the 'collect' example and there is no mention of mutex_trylock. Is that on purpose?

Thanks!

Timo

Timo Rozendal's icon

Hello vanille,

you're right
thanks for the response, sorry for my delay in responding

I did a little test and yes, the mutex_lock sleeps the current thread. Another thread can still unlock it.

Timo

Timo Rozendal's icon

curious: what is your recipe for delaying the message inside the external?

AlexHarker's icon

FWIW I also use lightweight thread sync stuff based on atomic operations a lot of the time, including when I need to wait till the lock is free. In this case you can spin on a while loop until it is free or similar . The thread will be consuming cycles, but that is often IMHO preferable to sleeping threads for short periods in real-time contexts...

A.

Timo Rozendal's icon

interesting, thanks guys.

alex, why is in your opinion spinning on a while loop preferred to sleeping a thread?
isn't that basically the same thing? or do I miss something.