Overview
The House Construction scenario simulates building a house with multiple workers where some tasks depend on others being completed first. This demonstrates thread join and await operations for managing task dependencies in concurrent programs.Real-World Problem
Building a house has natural dependencies:- Walls cannot be built until the foundation is complete
- The roof requires walls to be finished first
- Installations (plumbing, electrical) need walls but not the roof
- The architect can only deliver the house when both roof and installations are done
Shared Resources
The shared resource is a house state object tracking:
- Progress for each construction stage (foundation, walls, roof, installations)
- Which worker owns each stage
- Completion status of each stage
- Overall house delivery status
Synchronization Algorithm
This scenario uses Join and Await operations:Dependent Tasks Join
Wall worker executes
JOIN_THREAD on foundation worker, blocking until foundation completesParallel Dependent Tasks
Once walls complete, both roof worker and installation worker can proceed in parallel
Architect Awaits Multiple Threads
Architect executes
AWAIT_ALL on roof and installation workers, blocking until both finishScenario Setup
houseScenario.js
Configuration Options
| Parameter | Description | Default |
|---|---|---|
durations.foundation | Cycles to build foundation | Minimum 1 |
durations.walls | Cycles to build walls | Minimum 1 |
durations.roof | Cycles to build roof | Minimum 1 |
durations.installations | Cycles for installations | Minimum 1 |
Dependency Graph
Example Execution Flow
Thread Instructions
Workers with dependencies:- JOIN_THREAD - Block until specified thread completes
- BUILD_STAGE - Perform construction work for this stage
- END - Thread terminates
- AWAIT_ALL - Block until all specified threads complete
- COMPLETE_HOUSE - Finalize and deliver the house
- END - Thread terminates
Join blocks one thread until another completes. Await blocks until multiple threads complete. Both enable expressing task dependencies without busy-waiting.
Join vs Await
| Operation | Syntax | Waits For |
|---|---|---|
| JOIN_THREAD | JOIN_THREAD(target) | Single thread to complete |
| AWAIT_ALL | AWAIT_ALL([t1, t2, ...]) | Multiple threads to complete |
Parallelism Opportunities
Key Learning Points
- Task Dependencies: Some tasks must complete before others can start
- Join Operation: Wait for a single thread to complete
- Await Operation: Wait for multiple threads to complete
- Parallelism: Independent tasks (roof + installations) run concurrently
- Critical Path: Longest dependency chain determines total time
- Coordination: JoinManager tracks thread completion for dependent threads
Common Patterns
Comparison with Other Primitives
| Primitive | Purpose | When to Use |
|---|---|---|
| Join | Wait for thread completion | Task dependencies |
| Barrier | Synchronize at checkpoint | Iterative phases |
| Mutex | Mutual exclusion | Protect shared data |
| Semaphore | Resource counting | Limited resources |
| Condition Variable | Wait for condition | Producer-consumer |
Optimization Considerations
To minimize total construction time:
- Identify the critical path (longest dependency chain)
- Maximize parallelism for independent tasks
- Balance workload across parallel tasks
- Minimize idle time for expensive resources