~increscent/c-examples

c-examples/pthread/pthread.c -rw-r--r-- 529 bytes
d08deaa3Robert Williams renamed condition variable example 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Hint: make sure you compile with -lpthread

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

void *my_thread(void *arg) {
    long sleeptime = (long)arg;

    sleep(sleeptime);
    printf("Thread id: %d\n", pthread_self());

    return NULL;
}

int main(int argc, char **argv) {
    pthread_t tid_1;
    pthread_t tid_2;

    pthread_create(&tid_1, NULL, my_thread, (void *)1);
    pthread_create(&tid_2, NULL, my_thread, (void *)2);

    pthread_join(tid_1, NULL);
    pthread_join(tid_2, NULL);

    return 0;
}