SystemVerilog OOP Mastery · All levels

No Destructors or GC APIs (Handle Lifetime Semantics): Exercises

Exercises for No Destructors or GC APIs (Handle Lifetime Semantics).

Practice exercises

Exercise 1

Audit a monitor class that opens files and mailboxes. Define a deterministic shutdown policy independent of garbage-collection timing.

Solution

diagram
Add shutdown() that closes file descriptors, drains/stops worker loops, and nulls owned handles; invoke shutdown() from all controlled termination paths.

Exercise 2

Rewrite a task with multiple return points so semaphore release is guaranteed.

Solution

diagram
Acquire once, route all returns through a single release block (or explicitly release in every branch), then return.

Related topics