Concurrency
The Concurrency module provides comprehensive concurrency and parallelism capabilities for Soul, including goroutines, channels, mutexes, wait groups, promises, and other synchronization primitives for building concurrent applications.Basic Concurrency
spawn - Execute function in goroutine
Spawn a function to run in a separate goroutine and return a promise:run - Fire-and-forget execution
Run a function in a goroutine without waiting for results:spawn with arguments
Pass arguments to spawned functions:Channels
createChannel - Create channels for communication
Create channels for goroutine communication:Channel operations
Use channels for producer-consumer patterns:Synchronization Primitives
Mutex - Mutual exclusion
Use mutexes for protecting shared resources:tryLock - Non-blocking lock
Try to acquire a lock without blocking:WaitGroup - Wait for multiple goroutines
Coordinate multiple goroutines:Promises
createPromise - Create promises
Create promises for async operations:Promise.all - Wait for all promises
Wait for multiple promises to complete:Promise.race - Wait for first promise
Wait for the first promise to complete:Timing and Timeouts
sleep - Pause execution
Sleep for a specified duration:createTimeout - Timeout operations
Create timeouts for operations:Timeout cancellation
Cancel timeouts manually:Advanced Concurrency Patterns
Worker Pool Pattern
Producer-Consumer with Multiple Producers
Fan-out/Fan-in Pattern
One-time Execution
createOnce - Ensure function runs once
Ensure a function runs only once across multiple goroutines:Error Handling in Concurrent Code
Promise error handling
Channel error handling
Complete Example: Concurrent Web Scraper
Best Practices
- Resource cleanup: Always clean up resources (close channels, unlock mutexes)
- Error handling: Handle errors in concurrent code properly
- Avoid race conditions: Use proper synchronization primitives
- Timeout operations: Use timeouts for potentially long-running operations
- Graceful shutdown: Implement proper shutdown mechanisms

