From Tomasz Żok

WprowadzenieDoInformatyki: Wątki

Wprowadzenie

Zalety i wady równoległości

Wątki w C

Szablon kodu

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

void *hello_world(void *data) {
    printf("Hello World from thread %d\n", (int) data);
}

int main() {
    int i, n;
    pthread_t *threads;

    scanf("%d", &n);
    threads = (pthread_t*) malloc(n * sizeof(pthread_t));

    for (i = 0; i < n; i++) {
        pthread_create(&threads[i], NULL, hello_world, (void*) i);
    }

    for (i = 0; i < n; i++) {
        pthread_join(threads[i], NULL);
    }

    free(threads);
    return 0;
}
Hello World from thread 0
Hello World from thread 3
Hello World from thread 2
Hello World from thread 1

Hello World from thread 1
Hello World from thread 0
Hello World from thread 2
Hello World from thread 3

Demonstracja liniowego przyspieszenia

#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void *iterate(void *data) {
    int i;
    int m = (int) data;

    printf("Counting to %d\n", m);

    for (i = 0; i < m; i++) {
        sqrt(i);
    }
}

int main() {
    int i, n;
    pthread_t *threads;

    scanf("%d", &n);
    threads = (pthread_t*) malloc(n * sizeof(pthread_t));

    for (i = 0; i < n; i++) {
        pthread_create(&threads[i], NULL, iterate, INT_MAX / n);
    }

    for (i = 0; i < n; i++) {
        pthread_join(threads[i], NULL);
    }

    free(threads);
    return 0;
}

Demonstracja problemów z brakiem synchronizacji

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

int sum = 0;

void *compute(void *data) {
    int i;

    for (i = 0; i < 1000; i++) {
        sum++;
    }
}

int main() {
    int i, n;
    pthread_t *threads;

    scanf("%d", &n);
    threads = (pthread_t*) malloc(n * sizeof(pthread_t));

    for (i = 0; i < n; i++) {
        pthread_create(&threads[i], NULL, compute, NULL);
    }

    for (i = 0; i < n; i++) {
        pthread_join(threads[i], NULL);
    }

    printf("%d\n", sum);

    free(threads);
    return 0;
}

Obliczanie liczby {$\pi$} metodą Monte Carlo

Rozwiązanie problemu producent-konsument

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

sem_t can_produce;
sem_t can_consume;

void *producer(void *data) {
    while (1) {
        sem_wait(&can_produce);
        puts("PRODUCING!");
        sem_post(&can_consume);
    }
}

void *consumer(void *data) {
    while (1) {
        sem_wait(&can_consume);
        puts("CONSUMING!");
        sem_post(&can_produce);
    }
}

int main(int argc, char **argv) {
    pthread_t thread_prod, thread_cons;

    sem_init(&can_produce, 0, 1);
    sem_init(&can_consume, 0, 0);

    pthread_create(&thread_prod, NULL, producer, NULL);
    pthread_create(&thread_cons, NULL, consumer, NULL);

    pthread_join(thread_cons, NULL);
    pthread_join(thread_prod, NULL);

    return 0;
}

Rozwiązanie problemu czytelników-pisarzy

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

#define MAX_READERS 5

int readers;
sem_t can_read;
sem_t can_write;
sem_t rcount;

void *writer(void *data) {
    while (1) {
        sem_wait(&can_write);
        puts("WRITING!");
        sem_post(&can_read);
    }
}

void *reader(void *data) {
    while (1) {
        sem_wait(&can_read);

        sem_wait(&rcount);
        readers++;
        if (readers < MAX_READERS) {
            sem_post(&can_read);
        }
        sem_post(&rcount);

        printf("READING! (id=%d)\n", (int) data);

        sem_wait(&rcount);
        readers--;
        if (readers == 0) {
            sem_post(&can_write);
        } else {
            sem_post(&can_read);
        }
        sem_post(&rcount);
    }
}

int main(int argc, char **argv) {
    int i;
    pthread_t thread_reader[MAX_READERS];
    pthread_t thread_writer;

    readers = 0;
    sem_init(&can_write, 0, 1);
    sem_init(&can_read, 0, 0);
    sem_init(&rcount, 0, 1);

    pthread_create(&thread_writer, NULL, writer, NULL);
    for (i = 0; i < MAX_READERS; i++) {
        pthread_create(&thread_reader[i], NULL, reader, (void*) i);
    }

    pthread_join(thread_writer, NULL);
    for (i = 0; i < MAX_READERS; i++) {
        pthread_join(thread_reader[i], NULL);
    }
    return 0;
}
Retrieved from http://www.cs.put.poznan.pl/tzok/wiki/index.php?n=WprowadzenieDoInformatyki.W%c4%85tki
Page last modified on 2017 Jan Mon 9 10:00