Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Linear search using Multi-threading in C
Linear search using multi-threading in C involves dividing an array into multiple segments and assigning each segment to a separate thread for searching. This parallel approach can improve search performance on multi-core systems by utilizing multiple CPU cores simultaneously.
Syntax
pthread_create(&thread_id, NULL, search_function, thread_data); pthread_join(thread_id, NULL);
Example
To compile and run this program, you need to link with the pthread library using:
gcc filename.c -lpthread
Here's how to implement multi-threaded linear search where each thread searches a specific portion of the array −
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define MAX 16
#define THREAD_MAX 4
int array[MAX] = {1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220};
int key = 18;
int found = 0;
int found_index = -1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct {
int thread_id;
int start_index;
int end_index;
} ThreadData;
void* ThreadSearch(void* args) {
ThreadData* data = (ThreadData*)args;
for (int i = data->start_index; i < data->end_index; i++) {
if (array[i] == key) {
pthread_mutex_lock(&mutex);
if (!found) {
found = 1;
found_index = i;
printf("Thread %d found key %d at index %d<br>", data->thread_id, key, i);
}
pthread_mutex_unlock(&mutex);
break;
}
}
return NULL;
}
int main() {
pthread_t threads[THREAD_MAX];
ThreadData thread_data[THREAD_MAX];
int segment_size = MAX / THREAD_MAX;
printf("Searching for key %d in array using %d threads<br>", key, THREAD_MAX);
/* Create threads */
for (int i = 0; i < THREAD_MAX; i++) {
thread_data[i].thread_id = i;
thread_data[i].start_index = i * segment_size;
thread_data[i].end_index = (i + 1) * segment_size;
pthread_create(&threads[i], NULL, ThreadSearch, &thread_data[i]);
}
/* Wait for all threads to complete */
for (int i = 0; i < THREAD_MAX; i++) {
pthread_join(threads[i], NULL);
}
if (found) {
printf("Key element %d found at index %d<br>", key, found_index);
} else {
printf("Key element %d not found in the array<br>", key);
}
pthread_mutex_destroy(&mutex);
return 0;
}
Output
Searching for key 18 in array using 4 threads Thread 1 found key 18 at index 7 Key element 18 found at index 7
How It Works
- The array is divided into equal segments based on the number of threads
- Each thread searches its assigned segment independently
- A mutex ensures thread-safe access to shared variables when an element is found
- The main thread waits for all search threads to complete using
pthread_join()
Key Points
- Use mutex locks to prevent race conditions when multiple threads access shared data
- Pass thread-specific data using structures to avoid conflicts
- Multi-threading provides better performance on large arrays with multiple CPU cores
- Always compile with
-lpthreadflag for pthread functionality
Conclusion
Multi-threaded linear search divides the workload among multiple threads, potentially reducing search time on multi-core systems. Proper synchronization using mutexes ensures thread safety and correct results.
