Mark Yourself Question
A worker sends exactly two simulation results through an initially empty, thread-safe queue that can hold one result. Putting a result into a full queue waits until space becomes available. Getting a result from an empty queue waits until a result becomes available.
The worker puts the first result, puts the second result, and then finishes. The coordinator first joins the worker, meaning it waits for the worker to finish, and only afterward gets the two results. There are no other consumers, timeouts, or failures.
a) Determine whether both activities can finish. Explain the waiting dependencies.
b) Keep the queue capacity at one and propose a coordinator operation order that allows both activities to finish, assuming each runnable activity eventually receives processor time.
c) Explain whether adding an application mutex around each queue operation would repair the original order.
Unlock full course notes by signing up
Elapsed time for a thermal simulation
A thermal simulation has 0.90 s0.90\ \text{s}0.90 s of serial work and a parallelizable section requiring 4.80 s4.80\ \text{s}4.80 s on one worker. Six workers divide the parallelizable section equally and execute simultaneously on six available cores. Starting and coordinating them adds 0.15 s0.15\ \text{s}0.15 s of elapsed-time overhead.
Assume the serial work, parallel work, and overhead do not overlap. What is the total elapsed time? Answer in seconds, to two decimal places.
Rechecking sample availability after waiting
Two analysis threads take samples from a shared buffer. The integer ready_count records how many samples are available, and all accesses to the buffer and count use the same mutex. A producer adds one sample at a time, increases the count and then notifies waiting threads.
A condition variable lets a thread wait for a possible change in shared state. Its wait operation atomically releases the mutex and blocks the calling thread; before returning, it reacquires the mutex.
Each consumer must never remove a sample from an empty buffer. While holding the mutex, begin the consumer's guard with the keyword ________, followed by the condition ready_count == 0 and the wait operation as its body. After the guard, the consumer removes one sample and decreases the count before releasing the mutex.
When additional workers pay off
An engineering calculation contains 0.60 s0.60\ \text{s}0.60 s of serial work and 5.40 s5.40\ \text{s}5.40 s of parallelizable work measured on one worker. A three-worker implementation adds 0.12 s0.12\ \text{s}0.12 s of total coordination overhead. A six-worker implementation adds an unknown total coordination overhead H6H_6H6.
For both implementations, assume enough cores, perfectly balanced parallel work, and no overlap between serial work, parallel work, and overhead. Which condition is necessary and sufficient for the six-worker implementation to be strictly faster than the three-worker implementation?
Possible outcomes of indivisible updates
An engineering program uses two threads to update a shared, dimensionless integer x, initially equal to 3. Each labelled operation is an atomic update: its read, calculation and write act as one indivisible step.
Thread A performs A1, x = x + 4, followed by A2, x = 2 * x. Thread B performs B1, x = x - 1, followed by B2, x = 3 * x.
Assume every execution places these four operations in one total order that preserves the order within each thread. Every such order is possible. All four operations finish exactly once, arithmetic is exact, and no other code accesses x. The main thread reads x only after both worker threads finish.