Binary Search using pthread in C Program?

Binary search is one of the most efficient searching algorithms for sorted arrays. It works by repeatedly dividing the search space in half. In this article, we'll explore how to implement binary search using multiple threads (pthread) in C to potentially improve performance by parallelizing the search across different segments of the array.

Syntax

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine) (void *), void *arg);
int pthread_join(pthread_t thread, void **retval);

Example: Multithreaded Binary Search

This approach divides the sorted array into equal segments and assigns each segment to a separate thread for parallel searching −

Note: To compile this program, you need to link with pthread library: gcc -o program program.c -lpthread

#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>

#define MAX 16
#define MAX_THREAD 4

// Global variables to share data between threads
int arr[] = {1, 6, 8, 11, 13, 14, 15, 19, 21, 23, 26, 28, 31, 65, 108, 220};
int key = 31;
bool found = false;
int part = 0;

void* binary_search(void* arg) {
    // Each thread takes 1/4th part of the array
    int thread_part = part++;
    int mid;
    int start = thread_part * (MAX / 4);
    int end = (thread_part + 1) * (MAX / 4);
    
    // Perform binary search on the assigned segment
    while (start < end && !found) {
        mid = (end - start) / 2 + start;
        if (arr[mid] == key) {
            found = true;
            break;
        }
        else if (arr[mid] > key)
            end = mid - 1;
        else
            start = mid + 1;
    }
    return NULL;
}

int main() {
    pthread_t threads[MAX_THREAD];
    
    // Create threads
    for (int i = 0; i < MAX_THREAD; i++) {
        pthread_create(&threads[i], NULL, binary_search, NULL);
    }
    
    // Wait for all threads to complete
    for (int i = 0; i < MAX_THREAD; i++) {
        pthread_join(threads[i], NULL);
    }
    
    // Display result
    if (found)
        printf("%d found in array<br>", key);
    else
        printf("%d not found in array<br>", key);
        
    return 0;
}
31 found in array

How It Works

  • Array Division: The sorted array is divided into equal segments (4 segments for 4 threads)
  • Parallel Execution: Each thread performs binary search on its assigned segment
  • Shared State: The global found variable allows threads to stop searching once the key is found
  • Thread Synchronization: pthread_join() ensures all threads complete before displaying results

Key Points

  • This approach works best when the key could be in any segment of the array
  • Race conditions on the part variable ensure each thread gets a unique segment
  • The found flag prevents unnecessary computation once the key is located
  • Performance improvement depends on the number of available CPU cores

Conclusion

Multithreaded binary search can provide performance benefits by utilizing multiple CPU cores. However, for small arrays, the overhead of thread creation might outweigh the benefits. This technique is most effective for large datasets where parallelization can significantly reduce search time.

Updated on: 2026-03-15T11:49:09+05:30

634 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements