pthreads mutex variable, briefly

The lock function [1] accepts an _address_ to a mutex object. So you can either have a ptr-to-mutex variable, or a nonref variable and pass its address. It’s hopeless to pass-by-value (pbclone) because multiple threads must share the same mutex instance, not copies of it.

Since address is needed everywhere, it’s slightly simpler to use a ptr, but don’t forget to free the malloc.

Typically you can’t avoid malloc, because a mutex object on stack is not very useful. A mutex is typically used by multiple threads.

[1] Actually all pthread mutex functions

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s