讀書心得 Advanced Linux Programming (4-1) Create a Thread

本文出自 Advanced Linux Programming- Mark Mitchell, Jeffrey Oldham and Alex Samuel


Chp4. Thread Creation


A simple example of creating thread:

//
// thread-create.c
//
#include <pthread.h>
#include <stdio.h>

void* print_xs(int *num) {
    while(1) {
        fputc('x',stderr);
    }
}

int main(){
    pthread_t thread_id;
    
    pthread_create (&thread_id, NULL, &print_xs, NULL);

    while (1){
        fputc('o',stderr);
    }
    return 0;
}

Compile and run the program with libpthread:

$ cc -o thread-create thread-create.c -lpthread

!!! 注意 'o' 和 'x' 是交互出現的 !!!

留言

熱門文章