From 502a67f96500c44ba2deffbe749051ae1bc51c28 Mon Sep 17 00:00:00 2001 From: Remzi Arpaci-Dusseau Date: Tue, 23 Apr 2019 12:12:17 -0500 Subject: [PATCH] some thread API examples --- threads-api/Makefile | 21 ++++++++++++ threads-api/README.md | 20 +++++++++++ threads-api/thread_create.c | 25 ++++++++++++++ threads-api/thread_create_simple_args.c | 19 +++++++++++ threads-api/thread_create_with_return_args.c | 36 ++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 threads-api/Makefile create mode 100644 threads-api/README.md create mode 100644 threads-api/thread_create.c create mode 100644 threads-api/thread_create_simple_args.c create mode 100644 threads-api/thread_create_with_return_args.c diff --git a/threads-api/Makefile b/threads-api/Makefile new file mode 100644 index 0000000..0ced5e3 --- /dev/null +++ b/threads-api/Makefile @@ -0,0 +1,21 @@ +CC := gcc +CFLAGS := -Wall -Werror -I../include + +SRCS := thread_create.c \ + thread_create_simple_args.c \ + thread_create_with_return_args.c + +OBJS := ${SRCS:c=o} +PROGS := ${SRCS:.c=} + +.PHONY: all +all: ${PROGS} + +${PROGS} : % : %.o Makefile + ${CC} $< -o $@ -pthread + +clean: + rm -f ${PROGS} ${OBJS} + +%.o: %.c Makefile + ${CC} ${CFLAGS} -c $< diff --git a/threads-api/README.md b/threads-api/README.md new file mode 100644 index 0000000..960b4cb --- /dev/null +++ b/threads-api/README.md @@ -0,0 +1,20 @@ + +# Threads API + +Some simple examples of how to use POSIX thread APIs. + +Relevant files: +- `thread_create.c`: A simple thread creation program, with args passed to the +thread. +- `thread_create_with_return_args.c`: Return values from the thread to the +parent. +- `thread_create_simple_args.c`: Simpler argument/return value passing for the +lazy. + +Type `make` to build; each file `foo.c` generates an executable `foo` which +you can then run, e.g.: + +```sh +prompt> ./thread_create +``` + diff --git a/threads-api/thread_create.c b/threads-api/thread_create.c new file mode 100644 index 0000000..0472e97 --- /dev/null +++ b/threads-api/thread_create.c @@ -0,0 +1,25 @@ +#include +#include +#include + +typedef struct { + int a; + int b; +} myarg_t; + +void *mythread(void *arg) { + myarg_t *args = (myarg_t *) arg; + printf("%d %d\n", args->a, args->b); + return NULL; +} + +int main(int argc, char *argv[]) { + pthread_t p; + myarg_t args = { 10, 20 }; + + int rc = pthread_create(&p, NULL, mythread, &args); + assert(rc == 0); + (void) pthread_join(p, NULL); + printf("done\n"); + return 0; +} diff --git a/threads-api/thread_create_simple_args.c b/threads-api/thread_create_simple_args.c new file mode 100644 index 0000000..4b6ce93 --- /dev/null +++ b/threads-api/thread_create_simple_args.c @@ -0,0 +1,19 @@ +#include +#include +#include "common_threads.h" + +void *mythread(void *arg) { + long long int value = (long long int) arg; + printf("%lld\n", value); + return (void *) (value + 1); +} + +int main(int argc, char *argv[]) { + pthread_t p; + long long int rvalue; + Pthread_create(&p, NULL, mythread, (void *) 100); + Pthread_join(p, (void **) &rvalue); + printf("returned %lld\n", rvalue); + return 0; +} + diff --git a/threads-api/thread_create_with_return_args.c b/threads-api/thread_create_with_return_args.c new file mode 100644 index 0000000..a8279d5 --- /dev/null +++ b/threads-api/thread_create_with_return_args.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include "common_threads.h" + +typedef struct { + int a; + int b; +} myarg_t; + +typedef struct { + int x; + int y; +} myret_t; + +void *mythread(void *arg) { + myarg_t *args = (myarg_t *) arg; + printf("args %d %d\n", args->a, args->b); + myret_t *rvals = malloc(sizeof(myret_t)); + assert(rvals != NULL); + rvals->x = 1; + rvals->y = 2; + return (void *) rvals; +} + +int main(int argc, char *argv[]) { + pthread_t p; + myret_t *rvals; + myarg_t args = { 10, 20 }; + Pthread_create(&p, NULL, mythread, &args); + Pthread_join(p, (void **) &rvals); + printf("returned %d %d\n", rvals->x, rvals->y); + free(rvals); + return 0; +} +