Skip to content

Commit 40dba27

Browse files
committed
P3833R1
Signed-off-by: Ted Lyngmo <ted@lyncon.se>
1 parent b9bd09a commit 40dba27

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

P3833/P3833.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,42 @@
22
title: "`std::multi_lock`"
33
---
44

5-
- **Document number:** P3833R1a
5+
- **Document number:** P3833R1
66
- **Date:** 2026-03-24
77
- **Audience:** LEWGI/SG1
88
- **Project:** ISO/IEC 14882 Programming Languages — C++, ISO/IEC JTC1/SC22/WG21
99
- **Reply-to:** Ted Lyngmo <ted@lyncon.se>
1010

11+
## Revision History
12+
13+
### R1
14+
15+
- Rewrote postconditions throughout the wording to use prose ("is equal to", "is `true`", "is `false`") instead of `==` notation.
16+
1117
## Abstract
1218

1319
This paper proposes `std::multi_lock`, a RAII class template that combines the functionality of `std::unique_lock` and `std::scoped_lock`. Unlike `std::scoped_lock`, which provides only basic RAII semantics, `std::multi_lock` offers the full flexibility of `std::unique_lock` (deferred locking, try-lock operations, timed locking, and ownership transfer) while supporting multiple mutexes simultaneously.
1420

1521
## Contents
1622

17-
1. [Motivation](#motivation)
18-
2. [Proposed Solution](#proposed-solution)
19-
3. [Impact on the Standard](#impact-on-the-standard)
20-
4. [Design Decisions](#design-decisions)
23+
1. [Revision History](#revision-history)
24+
2. [Motivation](#motivation)
25+
3. [Proposed Solution](#proposed-solution)
26+
4. [Impact on the Standard](#impact-on-the-standard)
27+
5. [Design Decisions](#design-decisions)
2128
- [Return value for try-lock operations](#return-value-for-try-lock-operations)
2229
- [Timed locking with multiple mutexes](#timed-locking-with-multiple-mutexes)
2330
- [Exception Safety](#exception-safety)
2431
- [Deadlock Avoidance](#deadlock-avoidance)
25-
5. [Technical Specification](#technical-specification)
32+
6. [Technical Specification](#technical-specification)
2633
- [Header `<mutex>` synopsis](#header-mutex-synopsis)
2734
- [Class template `multi_lock`](#class-template-multi_lock)
28-
6. [Alternative Designs Considered](#alternative-designs-considered)
29-
7. [Implementation Experience](#implementation-experience)
30-
8. [Wording](#wording)
31-
9. [References](#references)
35+
7. [Alternative Designs Considered](#alternative-designs-considered)
36+
8. [Implementation Experience](#implementation-experience)
37+
9. [Wording](#wording)
38+
10. [References](#references)
3239

33-
## 1. Motivation
40+
## 2. Motivation
3441

3542
The C++ standard library provides several mutex wrapper classes:
3643

@@ -58,7 +65,7 @@ The following table illustrates how `multi_lock` completes the family of mutex w
5865

5966
(*) Supports deferred locking, time-constrained attempts at locking, recursive locking, and transfer of lock ownership.
6067

61-
## 2. Proposed Solution
68+
## 3. Proposed Solution
6269

6370
This paper proposes `std::multi_lock<Ms...>`, a variadic class template that:
6471

@@ -123,11 +130,11 @@ for(int rv; (rv = lock.try_lock_for(100ms)) != -1;) {
123130
// critical section
124131
```
125132
126-
## 3. Impact on the Standard
133+
## 4. Impact on the Standard
127134
128135
This proposal is a pure library extension. It adds a new class template `std::multi_lock` to the `<mutex>` header and updates related sections of the standard to reference it. There are no changes to the core language and no breaking changes to existing code.
129136
130-
## 4. Design Decisions
137+
## 5. Design Decisions
131138
132139
### Return value for try-lock operations
133140
@@ -159,7 +166,7 @@ When locking multiple mutexes, `std::multi_lock` must avoid deadlock. The implem
159166
- For `try_lock()`: Attempts to lock mutexes in order and backs off if any lock fails. It can use `std::try_lock(*get<Is>(pm)...)` for this purpose.
160167
- For `try_lock_until()` and `try_lock_for()`: It can use an algorithm similar to `std::lock()` but with timed operations, as proposed in P3832.
161168
162-
## 5. Technical Specification
169+
## 6. Technical Specification
163170
164171
### Header `<mutex>` synopsis
165172
@@ -237,7 +244,7 @@ namespace std {
237244
}
238245
```
239246
240-
## 6. Alternative Designs Considered
247+
## 7. Alternative Designs Considered
241248
242249
### Container-based Interface
243250
@@ -256,11 +263,11 @@ multi_lock(std::span<Mutex*> mutexes);
256263
257264
**Rejected:** Both approaches require all mutexes to be of the same type. While `std::span` avoids ownership concerns and could be non-owning, it still cannot mix different mutex types (e.g., `std::mutex` and `std::timed_mutex` in the same lock). The variadic template approach maintains type safety, allows heterogeneous mutex types, and enables compile-time optimizations that would not be possible with a runtime container. Additionally, it is probably preferable to keep the interface similar to that of `unique_lock` and `scoped_lock` to complete the family of mutex wrapper classes with a consistent design.
258265
259-
## 7. Implementation Experience
266+
## 8. Implementation Experience
260267
261268
A complete implementation is available at [github.com/bemanproject/timed_lock_alg](https://github.com/bemanproject/timed_lock_alg). The implementation has been tested with multiple mutex types and demonstrates that the design is implementable and practical.
262269
263-
## 8. Wording
270+
## 9. Wording
264271
265272
The following changes are relative to N5014.
266273
@@ -568,7 +575,7 @@ mutex_type mutex() const noexcept;
568575

569576
**3** *Returns*: `pm`.
570577

571-
## 9. References
578+
## 10. References
572579

573580
- N5014: Working Draft, Standard for Programming Language C++
574581
- [P0156R2](https://wg21.link/p0156r2): Variadic `lock_guard` (scoped_lock)

0 commit comments

Comments
 (0)