Design a reusable signaling primitive that lets threads wait for a newer version.
Implement the VersionedSignal class:
VersionedSignal() creates a signal whose version starts at 0.currentVersion() returns the current version.signal() increments the version by one and wakes every waiter that may now proceed.awaitNext(observedVersion) waits until the current version is strictly greater than observedVersion, then returns the current version.
If the version is already greater than observedVersion, awaitNext must return immediately. This makes signals persistent: a signal that occurs before a waiter starts must not be lost.
All methods may be called concurrently. Every waiter must re-check its predicate after waking because wake-ups can be spurious and one notification may not advance the version far enough for every waiter.
The judge creates all signaler and waiter threads. Standard concurrency, condition-variable, and thread APIs are preloaded, so you do not need import, include, package, or using statements.
Example 1:
Input:
Output:
Explanation: The waiter observes version 0, blocks, and returns after the signal advances the version to 1.
Example 2:
Input:
Output:
Explanation: Both signals happened first, but their state is preserved in the version. The call does not wait for another notification.
Constraints
0 <= observedVersion <= 100000- At most
100000 calls to signal are made per instance. - At most
100 threads wait on one instance at a time. - Every waiting call eventually observes a version greater than its argument.
- Judge threads are not interrupted while blocked in
awaitNext.