Multi-threading

Understanding Concurrent Programming in C

Introduction to Multi-threading (मल्टी-थ्रेडिंग का परिचय)

Multi-threading एक program को multiple threads में execute करने की capability है। हर thread independent execution flow होता है।

Multi-threading is the capability of a program to execute in multiple threads. Each thread is an independent execution flow.

Key Points (मुख्य बिंदु):

  • Threads parallel execution enable करते हैं
  • Threads enable parallel execution
  • Threads shared resources access कर सकते हैं
  • Threads can access shared resources
  • Thread synchronization important है
  • Thread synchronization is important
Main Thread
Worker Thread 1
Worker Thread 2
Synchronization

Thread Creation (थ्रेड क्रिएशन)

Threads create करने के तरीके:

Ways to create threads:

#include <pthread.h>

// Thread function
void* thread_function(void* arg) {
    int* value = (int*)arg;
    printf("Thread value: %d\n", *value);
    return NULL;
}

int main() {
    pthread_t thread;
    int value = 42;
    
    // Create thread
    pthread_create(&thread, NULL, thread_function, &value);
    
    // Wait for thread completion
    pthread_join(thread, NULL);
    
    return 0;
}
Function (फंक्शन) Description (विवरण)
pthread_create() New thread create करता है
pthread_create() Creates a new thread
pthread_join() Thread completion का wait करता है
pthread_join() Waits for thread completion
pthread_exit() Thread को terminate करता है
pthread_exit() Terminates a thread

Thread Synchronization (थ्रेड सिंक्रोनाइजेशन)

Thread synchronization के mechanisms:

Thread synchronization mechanisms:

#include <pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int shared_data = 0;

void* thread_function(void* arg) {
    pthread_mutex_lock(&mutex);
    shared_data++;
    pthread_mutex_unlock(&mutex);
    return NULL;
}

Synchronization Methods (सिंक्रोनाइजेशन मेथड्स):

  • Mutex locks
  • Mutex locks
  • Semaphores
  • Semaphores
  • Condition variables
  • Condition variables

Thread Safety (थ्रेड सेफ्टी)

Thread-safe programming के principles:

Principles of thread-safe programming:

// Thread-safe counter implementation
typedef struct {
    pthread_mutex_t mutex;
    int count;
} Counter;

void increment(Counter* counter) {
    pthread_mutex_lock(&counter->mutex);
    counter->count++;
    pthread_mutex_unlock(&counter->mutex);
}

int get_count(Counter* counter) {
    pthread_mutex_lock(&counter->mutex);
    int count = counter->count;
    pthread_mutex_unlock(&counter->mutex);
    return count;
}
  • Mutual Exclusion: Shared resources को protect करें
  • Mutual Exclusion: Protect shared resources
  • Atomic Operations: Atomic operations का use करें
  • Atomic Operations: Use atomic operations
  • Deadlock Prevention: Deadlock से बचें
  • Deadlock Prevention: Avoid deadlocks
  • Resource Management: Resources को properly manage करें
  • Resource Management: Manage resources properly
  • Best Practices (सर्वोत्तम प्रथाएं)

    Multi-threading के best practices:

    Best practices for multi-threading:

  • Thread Count: Optimal thread count maintain करें
  • Thread Count: Maintain optimal thread count
  • Error Handling: Thread errors को handle करें
  • Error Handling: Handle thread errors
  • Resource Cleanup: Resources को cleanup करें
  • Resource Cleanup: Cleanup resources
  • Testing: Thread behavior को test करें
  • Testing: Test thread behavior