Skip to content

Pirate-960/Threads-Synchronization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧵 Threads-Synchronization

C Multithreading PThread

Efficient multithreaded computation of square root sums with advanced synchronization methods


📌 Table of Contents


🚀 Project Overview

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.

✨ Features

  • 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:
    1. 🚫 No synchronization (for demonstration purposes)
    2. 🔒 Mutex-protected critical sections
    3. 🏎️ 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.

📋 Requirements

  • GCC Compiler
  • POSIX Threads (PThread) library
  • Math library

🛠 Compilation

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.

📖 Usage

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.

🔄 Synchronization Methods

  1. No Synchronization (Method 1):

    • Demonstrates the risks of race conditions.
    • Fastest but potentially inaccurate results.
  2. Mutex-protected Critical Sections (Method 2):

    • Uses a mutex to protect each update to the global sum.
    • Guarantees accuracy but may introduce significant overhead.
  3. Local Computation with Protected Global Sum (Method 3):

    • Each thread computes a local sum, then updates the global sum once.
    • Balances performance and accuracy.

📊 Performance Insights

Method Performance Accuracy Use Case
1 🚀🚀🚀 Demonstration of race conditions
2 🚀 ✅✅ When absolute accuracy is critical
3 🚀🚀 Best balance of speed and accuracy

⚠️ Limitations

  • 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

🤝 Contributing

Contributions to improve the project are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Made with ❤️

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages