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

add thread demo

This commit is contained in:
Stefan Lankes 2015-08-09 14:24:24 +02:00
parent eea712c218
commit 5087d28b8a
2 changed files with 41 additions and 4 deletions

View file

@ -1,7 +1,7 @@
ARCH = x86
TARGET=x86_64-hermit
MAKE = make
STRIP_DEBUG = --strip-debug
override STRIP_DEBUG = --strip-unneeded --strip-debug
KEEP_DEBUG = --only-keep-debug
# Set your own cross compiler tool chain prefix here
@ -37,7 +37,7 @@ endif
default: all
all: hello jacobi stream
all: hello jacobi stream thr_hello
stream: stream.o
@echo [LD] $@
@ -60,13 +60,20 @@ jacobi: jacobi.o
$Q$(OBJCOPY_FOR_TARGET) $(STRIP_DEBUG) $@
$Qchmod a-x $@.sym
thr_hello: thr_hello.o
@echo [LD] $@
$Q$(CC_FOR_TARGET) $(LDFLAGS) $(CFLAGS) -o $@ $< -lpthread
$Q$(OBJCOPY_FOR_TARGET) $(KEEP_DEBUG) $@ $@.sym
$Q$(OBJCOPY_FOR_TARGET) $(STRIP_DEBUG) $@
$Qchmod a-x $@.sym
clean:
@echo Cleaning examples
$Q$(RM) hello jacobi *.sym *.o *~
$Q$(RM) hello jacobi stream thr_hello *.sym *.o *~
veryclean:
@echo Propper cleaning examples
$Q$(RM) hello jacobi stream *.sym *.o *~
$Q$(RM) hello jacobi stream thr_hello *.sym *.o *~
depend:
$Q$(CC_FOR_TARGET) -MM $(CFLAGS) *.c > Makefile.dep

View file

@ -0,0 +1,30 @@
#include <stdio.h>
#include <pthread.h>
#define MAX_THREADS 2
void* thread_func(void* arg)
{
int id = *((int*) arg);
printf("Hello Thread!!! id = %d\n", id);
return 0;
}
int main(int argc, char** argv)
{
pthread_t threads[MAX_THREADS];
int i, param[MAX_THREADS];
for(i=0; i<MAX_THREADS; i++) {
param[i] = i;
pthread_create(threads+i, NULL, thread_func, param+i);
}
/* wait until all threads have terminated */
for(i=0; i<MAX_THREADS; i++)
pthread_join(threads[i], NULL);
return 0;
}