Efficient multithreaded computation of square root sums with advanced synchronization methods
- Project Overview
- Features
- Requirements
- Compilation
- Usage
- Synchronization Methods
- Performance Insights
- Limitations
- Contributing
The Threads-Synchronization project is a high-performance C program designed to efficiently compute the sum of square roots within a specified range using multithreading. It demonstrates the power of concurrent programming by utilizing the PThread library to distribute tasks across multiple threads, improving performance while ensuring proper synchronization.
- Multithreaded Execution: Utilizes up to 32 threads for parallel computation.
- Flexible Range: Supports computation for any range within [0, 9223372036854775807].
- Multiple Synchronization Methods: Implements three distinct approaches:
- 🚫 No synchronization (for demonstration purposes)
- 🔒 Mutex-protected critical sections
- 🏎️ Local computation with mutex-protected global sum update
- Performance Metrics: Provides detailed timing information for analysis.
- Thread-specific Information: Displays range and results for each thread.
- GCC Compiler
- POSIX Threads (PThread) library
- Math library
Compile the program using the following command:
gcc -o threads_sync threads_sync.c -lm -pthread
This command includes the math (-lm
) and pthread (-pthread
) libraries required for the program.
Run the compiled program with the following command:
./threads_sync <a> <b> <num_threads> <method>
Where:
<a>
: Start of the range (must be ≥ 0)<b>
: End of the range (must be ≤ 9223372036854775807)<num_threads>
: Number of threads to use (1-32)<method>
: Synchronization method (1, 2, or 3)
Example:
./threads_sync 1 1000000 4 3
This calculates the sum of square roots from 1 to 1,000,000 using 4 threads and method 3.
-
No Synchronization (Method 1):
- Demonstrates the risks of race conditions.
- Fastest but potentially inaccurate results.
-
Mutex-protected Critical Sections (Method 2):
- Uses a mutex to protect each update to the global sum.
- Guarantees accuracy but may introduce significant overhead.
-
Local Computation with Protected Global Sum (Method 3):
- Each thread computes a local sum, then updates the global sum once.
- Balances performance and accuracy.
Method | Performance | Accuracy | Use Case |
---|---|---|---|
1 | 🚀🚀🚀 | ❌ | Demonstration of race conditions |
2 | 🚀 | ✅✅ | When absolute accuracy is critical |
3 | 🚀🚀 | ✅ | Best balance of speed and accuracy |
- Maximum range: [0, 9223372036854775807]
- Maximum number of threads: 32 (can be adjusted in the code)
- Potential for floating-point precision issues with very large sums
Contributions to improve the project are welcome! Please follow these steps:
- Fork the repository
- Create a new branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Made with ❤️