snipplets.dev/code/C/thread.c

32 lines
477 B
C

/* https://www.geeksforgeeks.org/multithreading-in-c/ */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int g = 0;
void *my_thread(void *vargp) {
int *id = (int *)vargp;
static int s = 0;
++s;
++g;
printf("Thread ID: %d, Static: %d, Global: %d\n", *id, ++s, ++g);
}
int main() {
pthread_t tid;
int i;
for (i = 0; i < 3; i++)
pthread_create(&tid, NULL, my_thread, (void *)&tid);
pthread_exit(NULL);
return 0;
}