Untitled

Anonymous
Plain Text
09/24/2025 9:01 AM
1.3 KB
18
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

// Function for addition thread
void *addThread(void *arg) {
    int *nums = (int *)arg;   // nums[0] = a, nums[1] = b
    int sum = nums[0] + nums[1];
    printf("Addition Thread: %d + %d = %d\n", nums[0], nums[1], sum);
    return NULL;
}

// Function for multiplication thread
void *multiplyThread(void *arg) {
    int *nums = (int *)arg;   // nums[0] = a, nums[1] = b
    int product = nums[0] * nums[1];
    printf("Multiplication Thread: %d * %d = %d\n", nums[0], nums[1], product);
    return NULL;
}

int main() {
    pthread_t addThreadID, mulThreadID;

    int numbers[2];
    numbers[0] = 10;   // First number
    numbers[1] = 5;    // Second number

    // Create addition thread
    if (pthread_create(&addThreadID, NULL, addThread, (void *)numbers) != 0) {
        fprintf(stderr, "Error creating addition thread\n");
        return 1;
    }

    // Create multiplication thread
    if (pthread_create(&mulThreadID, NULL, multiplyThread, (void *)numbers) != 0) {
        fprintf(stderr, "Error creating multiplication thread\n");
        return 1;
    }

    // Wait for threads to finish
    pthread_join(addThreadID, NULL);
    pthread_join(mulThreadID, NULL);

    printf("Main thread finished execution.\n");

    return 0;
}
Editor is loading...
Leave a Comment