1
0
Fork 0
mirror of https://github.com/hermitcore/libhermit.git synced 2025-03-09 00:00:03 +01:00
libhermit/usr/tests/thr_hello.c

38 lines
719 B
C
Raw Permalink Normal View History

2015-08-09 14:24:24 +02:00
#include <stdio.h>
#include <pthread.h>
#define MAX_THREADS 2
__thread int id = -1;
2015-08-09 14:24:24 +02:00
void* thread_func(void* arg)
{
id = *((int*) arg);
2015-08-09 14:24:24 +02:00
printf("Hello thread!!! id = %d\n", id);
2015-08-09 14:24:24 +02:00
return 0;
}
int main(int argc, char** argv)
{
pthread_t threads[MAX_THREADS];
int param[MAX_THREADS];
2015-08-09 14:24:24 +02:00
printf("Hello form main thread! id = %d\n", id);
for(int i=0; i<MAX_THREADS; i++) {
2015-08-09 14:24:24 +02:00
param[i] = i;
int ret = pthread_create(threads+i, NULL, thread_func, param+i);
if (ret) {
2015-08-17 12:55:02 +02:00
printf("Thread creation failed! error = %d\n", ret);
return ret;
} else printf("Create thread %d\n", i);
2015-08-09 14:24:24 +02:00
}
/* wait until all threads have terminated */
for(int i=0; i<MAX_THREADS; i++)
2015-08-09 14:24:24 +02:00
pthread_join(threads[i], NULL);
return 0;
}