Terminating a secondary thread from the main thread in C or C++ using the POSIX pthread library can be accomplished using various techniques, depending on how gracefully you want to handle the termination process. Below are some common methods to terminate a secondary thread from the main thread:
1. Using a Cancellation Point
POSIX threads library provides mechanisms to request thread cancellation. The thread must periodically check for cancellation requests at specific points called cancellation points.
Example:
Main Thread:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *thread_function(void *arg) {
while (1) {
printf("Thread is running...\n");
sleep(1); // This is a cancellation point
// Other work to be done by the secondary thread
}
pthread_exit(NULL);
}
int main() {
pthread_t thread;
// Create a secondary thread
pthread_create(&thread, NULL, thread_function, NULL);
// Let the thread run for a while
sleep(5);
// Request cancellation of the secondary thread
pthread_cancel(thread);
// Ensure the main thread waits for the secondary thread to terminate
pthread_join(thread, NULL);
printf("Secondary thread has been terminated.\n");
return 0;
}
2. Using a Shared Variable
Instead of forcefully terminating the thread, you can use a shared variable that the secondary thread checks periodically to determine whether it should exit.
Example:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
// Shared variable to control termination
bool keep_running = true;
void *thread_function(void *arg) {
while (keep_running) {
printf("Thread is running...\n");
sleep(1); // Simulate work
// Other work to be done by the secondary thread
}
printf("Thread is terminating...\n");
pthread_exit(NULL);
}
int main() {
pthread_t thread;
// Create a secondary thread
pthread_create(&thread, NULL, thread_function, NULL);
// Let the thread run for a while
sleep(5);
// Set the shared variable to false
keep_running = false;
// Ensure the main thread waits for the secondary thread to terminate
pthread_join(thread, NULL);
printf("Secondary thread has been terminated.\n");
return 0;
}
3. Using Signals
You can also use signals to notify the secondary thread to terminate. However, this might require signal handling setup in the thread.
Example:
Signal Handler in Secondary Thread:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void signal_handler(int signum) {
pthread_exit(0);
}
void *thread_function(void *arg) {
// Set up signal handler to handle termination signal
signal(SIGUSR1, signal_handler);
while (1) {
printf("Thread is running...\n");
sleep(1); // Simulate work
// Other work to be done by the secondary thread
}
}
int main() {
pthread_t thread;
// Create a secondary thread
pthread_create(&thread, NULL, thread_function, NULL);
// Let the thread run for a while
sleep(5);
// Send a signal to the secondary thread
pthread_kill(thread, SIGUSR1);
// Ensure the main thread waits for the secondary thread to terminate
pthread_join(thread, NULL);
printf("Secondary thread has been terminated.\n");
return 0;
}
Summary
Different scenarios call for different techniques. If the thread must perform cleanup operations before terminating, using a shared variable or signal handling might be preferable. If a more forceful approach is acceptable and the secondary thread frequently reaches cancellation points (like sleep), using pthread_cancel can be effective. Each method has its own benefits and trade-offs, so choose the one that best suits your application's requirements.