thread example
This commit is contained in:
parent
a390ee355e
commit
4026c398e3
31
~/C/thread.c
Normal file
31
~/C/thread.c
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user