PoKeys Thread - Advanced Threading System
Advanced threading architecture for multi-device PoKeys applications. This library provides thread-safe multi-device management with per-device threads, state synchronization, and concurrent operations.
โจ Key Features
Multi-Device Threading
- Per-Device Threads: Each device operates in its own dedicated thread
- Thread Safety: Safe concurrent access to device resources
- State Synchronization: Automatic state management across threads
- Output State Tracking: Comprehensive tracking of device output states
Concurrent Operations
- Non-Blocking Operations: Device operations don't block other devices
- Parallel Processing: Multiple devices can be controlled simultaneously
- Event-Driven Architecture: Responsive to device state changes
- Resource Management: Automatic cleanup and resource management
Performance Benefits
- Scalability: Efficiently handles multiple devices
- Responsiveness: Real-time device interaction
- Isolation: Device failures don't affect other devices
- Load Distribution: Balanced workload across threads
๐ Quick Start
Add this to your Cargo.toml:
[]
= { = "https://github.com/pokeys-toolkit/thread" }
= { = "https://github.com/pokeys-toolkit/core" }
๐ Usage Examples
Basic Multi-Device Threading
use *;
State Observer Pattern
use *;
Device Operations
use *;
๐๏ธ Architecture
Thread Controller
The ThreadController is the main entry point for managing device threads:
- Device Discovery: Automatic USB and network device discovery
- Thread Management: Start, stop, and monitor device threads
- Operation Dispatch: Route operations to appropriate device threads
- State Management: Centralized state tracking and synchronization
Device Threads
Each device runs in its own dedicated thread:
- Isolated Execution: Device operations don't interfere with each other
- Automatic Refresh: Periodic device state updates
- Error Handling: Per-device error isolation and recovery
- Resource Cleanup: Automatic resource management on thread termination
State Synchronization
Thread-safe state management:
- Atomic Operations: Lock-free state updates where possible
- Consistent Views: Guaranteed consistent state snapshots
- Change Notifications: Event-driven state change notifications
- Output Tracking: Comprehensive output state tracking
๐ง Configuration
Thread Controller Builder
let controller = new
.default_refresh_interval // 50ms refresh rate
.max_retry_attempts // Retry failed operations
.timeout_duration // 5 second timeout
.enable_logging // Enable debug logging
.build;
Per-Device Configuration
controller.configure_device_thread;
๐ Examples
The examples/ directory contains comprehensive examples:
# Simple controller setup
# State observation patterns
# Device operation examples
# Logging and debugging
# Comprehensive multi-device example
๐ก๏ธ Safety & Reliability
Thread Safety
- Lock-Free Operations: Minimal locking for maximum performance
- Deadlock Prevention: Careful lock ordering and timeout mechanisms
- Resource Protection: Protected access to shared device resources
Error Handling
- Isolated Failures: Device errors don't propagate to other devices
- Automatic Recovery: Built-in retry and recovery mechanisms
- Graceful Degradation: System continues operating with failed devices
Performance
- Minimal Overhead: Efficient thread management and communication
- Scalable Architecture: Handles dozens of devices efficiently
- Memory Efficient: Careful memory management and cleanup
๐งช Testing
Run the test suite:
# Unit tests
# Integration tests (requires hardware)
# Performance benchmarks
๐ค Contributing
We welcome contributions! Please ensure:
- All tests pass
- Code follows Rust conventions
- Thread safety is maintained
- Documentation is updated
๐ License
This project is licensed under the LGPL-2.1 License - see the LICENSE file for details.
๐ Related Projects
- PoKeys Core - Core library
- PoKeys CLI - Command-line interface
- PoKeys Model Manager - Device model management
Perfect for: Industrial automation, multi-device control systems, responsive GUI applications, and any scenario requiring concurrent PoKeys device management.