aboutsummaryrefslogtreecommitdiff
path: root/tests/pthread.c
diff options
context:
space:
mode:
authortoni <toni@devlap.local>2015-11-17 16:35:01 +0100
committertoni <matzeton@googlemail.com>2015-11-17 17:12:05 +0100
commite674872a17825b6a9e48464ff80ced9b3e44ec46 (patch)
treeae503abaf96efac400e2fcff6f2a41cd9ef7bb6c /tests/pthread.c
parentecd71a9af7f74d1cd73ef015b767aa63dc0605d9 (diff)
- pthread test
Diffstat (limited to 'tests/pthread.c')
-rw-r--r--tests/pthread.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/pthread.c b/tests/pthread.c
new file mode 100644
index 0000000..eed53a6
--- /dev/null
+++ b/tests/pthread.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <sys/time.h>
+#include <errno.h>
+#include <assert.h>
+
+
+static pthread_mutex_t testMutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t testCond = PTHREAD_COND_INITIALIZER;
+
+static void
+mywait(int time_secs)
+{
+ struct timespec now;
+ int rt;
+
+ clock_gettime(CLOCK_REALTIME, &now);
+ now.tv_sec += time_secs;
+
+ assert( pthread_mutex_lock(&testMutex) == 0 );
+ rt = pthread_cond_timedwait(&testCond, &testMutex, &now);
+ assert( rt == ETIMEDOUT || rt == 0 );
+ assert( pthread_mutex_unlock(&testMutex) == 0 );
+ printf("Done.\n");
+}
+
+static void *
+fun(void *arg)
+{
+ printf("Thread wait ..\n");
+ mywait(1);
+ mywait(1);
+ return NULL;
+}
+
+int
+main(void)
+{
+ pthread_t thread;
+ void *ret;
+
+ assert( pthread_create(&thread, NULL, fun, NULL) == 0 );
+ usleep(50000);
+ assert( pthread_cond_signal(&testCond) == 0 );
+ printf("Mainthread: join\n");
+ assert( pthread_join(thread, &ret) == 0 );
+ return 0;
+}