程式語言 - GNU - C/C++ - Thread



參考資訊:
https://www.geeksforgeeks.org/multithreading-in-c/
https://www.cntofu.com/book/46/gcc/gcc4e2d-_pthread_548c-_lpthread_de_qu_bie.md

main.c

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

static void* mythread(void *param)
{
    printf("mythread++\n");
    sleep(1);
    printf("mythread--\n");
    return NULL;
}

int main(int argc, char **argv)
{
    pthread_t thread_id = 0;

    pthread_create(&thread_id, NULL, mythread, NULL);
    pthread_join(thread_id, NULL);
    return 0;
}

執行

$ gcc main.c -o main -pthread
$ ./main 
    mythread++
    mythread--