threads


1. Creating and Joining Threads

#include <pthread.h>

void* thread_func(void* arg) {
    printf("Hello from thread!\n");
    return NULL;
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_func, NULL);
    pthread_join(thread, NULL);
    return 0;
}

2. Concurrent Task Execution

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

void* thread_func1(void* arg) {
    for (int i = 0; i < 10000; i++) {
        printf("Thread 1: %d\n", i);
    }
    return NULL;
}

void* thread_func2(void* arg) {
    for (int i = 0; i < 10000; i++) {
        printf("Thread 2: %d\n", i);
    }
    return NULL;
}

int main() {
    pthread_t thread1, thread2;
    pthread_create(&thread1, NULL, thread_func1, NULL);
    pthread_create(&thread2, NULL, thread_func2, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    return 0;
}

3. Shared Data Access: Mutex

4. Shared Data Access: Semaphore

5. Condition Variables

6. Producer-Consumer

7. Dining Philosophers

8. Read-Write Lock

9. Thread Priority

10. Thread-Specific Data

11. Barrier Synchronization

12. Once Initialization

13. Thread Local Storage