pthread_pause pthread_unpause
[mirrors/Programs.git] / c / pthread_extra / test.c
CommitLineData
3aac2619
TM
1#include <pthread.h>
2#include <pthread_extra.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <assert.h>
6#include <unistd.h>
7
8
9int main_mumu() {
10 //Prepare mutex array for tests
11 static pthread_mutex_t la = PTHREAD_MUTEX_INITIALIZER;
12 static pthread_mutex_t lb = PTHREAD_MUTEX_INITIALIZER;
13 static pthread_mutex_t lc = PTHREAD_MUTEX_INITIALIZER;
14 static pthread_mutex_t ld = PTHREAD_MUTEX_INITIALIZER;
15 static pthread_mutex_t *lck[4] = {&la, &lb, &lc, &ld};
16
17 //Set timeout
18 struct timespec tm;
19 clock_gettime(CLOCK_REALTIME, &tm);
5251b63c 20 tm.tv_sec += 2;
3aac2619
TM
21
22 //Lock one of the locks for testing
23 pthread_mutex_lock(lck[2]);
24
25 if(!pthread_mutex_timedlock_multi(lck, 4, &tm)) {
26 //if(!pthread_mutex_timedlock_two(&la, lck[2], &tm)) {
27 printf("LOCKED!\n");
28 } else {
29 printf("FAILED!\n");
30 }
31
5251b63c 32 return 0;
3aac2619
TM
33}
34
35
36pthread_mq_t myq;
37
5251b63c
TM
38void *thread_recv() {
39 //(void *)args;
40
3aac2619
TM
41 char str[128];
42 while(1) {
a8e71e8f
TM
43 if(pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_FOREVER))
44 printf("-RECVD: %.6s\t\t(waiting %d)\n", str, (int)pthread_mq_waiting(&myq));
45 if(pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_NOBLOCK))
46 printf("+RECVD: %.6s\t\t(waiting %d)\n", str, (int)pthread_mq_waiting(&myq));
3aac2619
TM
47 }
48}
49
a8e71e8f
TM
50void *thread_send() {
51 while(1) {
52 pthread_mq_send_generic(&myq, " X ", true, PTHREAD_XTIME_NOBLOCK);
53 pthread_mq_send_generic(&myq, " Y ", false, PTHREAD_XTIME_FOREVER);
54 }
55}
56
57
3aac2619 58int main() {
5251b63c 59 //main_mumu();
3aac2619 60
5251b63c 61 //char tmp[128];
3aac2619
TM
62
63 pthread_mq_init(&myq, 6, 5);
64
65 pthread_t t;
66 pthread_create(&t, NULL, thread_recv, NULL);
a8e71e8f
TM
67 pthread_t s;
68 pthread_create(&s, NULL, thread_send, NULL);
3aac2619
TM
69
70 pthread_mq_send_generic(&myq, "AHOJ1", false, NULL);
71 pthread_mq_send_generic(&myq, "AHOJ2", false, NULL);
72 pthread_mq_send_generic(&myq, "AHOJ3", true, NULL);
73 pthread_mq_send_generic(&myq, "AHOJ4", true, NULL);
74 pthread_mq_send_generic(&myq, "AHOJ5", false, NULL);
75 pthread_mq_send_generic(&myq, "AHOJ6", true, NULL);
76
77 while(1) {
a8e71e8f
TM
78 pthread_mq_send_generic(&myq, "B ", false, NULL);
79 pthread_mq_send_generic(&myq, "A ", true, NULL);
80 pthread_mq_send_generic(&myq, " B ", false, PTHREAD_XTIME_FOREVER);
81 pthread_mq_send_generic(&myq, " A ", false, PTHREAD_XTIME_NOBLOCK);
3aac2619
TM
82 }
83
84 pthread_join(t, NULL);
85}
86
This page took 0.228933 seconds and 4 git commands to generate.