# std: halve the number of mutexes per mutex · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/4f16e80ceadc19c50c5be67dd9cc2c2f9aa4beb8)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 4f16e80ceadc19c50c5be67dd9cc2c2f9aa4beb8

Requested commit: 4f16e80ceadc19c50c5be67dd9cc2c2f9aa4beb8

Commit: 4f16e80ceadc19c50c5be67dd9cc2c2f9aa4beb8

Tree: c9e918e2a521a053472970907730921cc866c356

Author: Matthew Lugg

Committer: Alex Rønne Petersen

## Message

```
std: halve the number of mutexes per mutex

On NetBSD and Illumos, we were using the `std.Thread.Futex`-based
implementation of `std.Thread.Mutex`. But since futex is not a primitive
on these targets, the implementation of `std.Thread.Futex` was based on
pthread primitives, including `pthread_mutex_t`. This had the amusing
consequence that locking a contended mutex on NetBSD would actually
perform 2 mutex locks, 2 mutex unlocks, and 1 condition wait; likewise,
unlocking a contended mutex would perform 2 mutex locks, 2 mutex
unlocks, and 1 condition signal. Having read some cutting-edge studies,
I have concluded that this is a slightly suboptimal approach. Instead,
let's just use pthread mutexes directly in this case; that's an
obviously better idea.

In the future, I think we can probably entirely remove our usages of
pthread sync primitives---no platform actually treats them as the base
primitives. Of the platforms which std has any meaningful support for
today, most support futexes, and the exceptions (NetBSD and Illumos)
support a thread parking API. We can implement futex and/or mutex on top
of thread parking and drop the pthread dependency entirely.

```

## Parents

- [c518593e9793a2aed4e0173348aff2cbef58b717](https://git.cafe/gitcafe/zig/commit/c518593e9793a2aed4e0173348aff2cbef58b717?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/4f16e80ceadc19c50c5be67dd9cc2c2f9aa4beb8?format=markdown)
