csci4061/notes/11-threads-code/pthread_ids.c
Michael Zhang 041f660ccd
f
2018-01-29 17:28:37 -06:00

23 lines
577 B
C

// Small example showing non-portable printing of thread IDs which are
// often just unsigned long
#include <pthread.h>
#include <stdio.h>
void *doit(void *param){
pthread_t tid = pthread_self();
printf("doit: I am thread %ld\n",tid);
int *var = (int *) param;
*var = (*var) * 2;
return NULL;
}
int main(){
pthread_t thread_1;
int stackvar = 42;
pthread_create(&thread_1, NULL, doit, &stackvar);
pthread_t tid = pthread_self();
printf("main: I am thread %ld\n",tid);
pthread_join(thread_1, NULL);
printf("stack var is: %d\n",stackvar);
return 0;
}