thread example

This commit is contained in:
Alexander Popov 2023-08-01 00:16:28 +03:00
parent a390ee355e
commit 4026c398e3
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6

31
~/C/thread.c Normal file
View File

@ -0,0 +1,31 @@
/* 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;
}