Refaktorace knihovny
[mirrors/Programs.git] / c / pthread_extra / test.c
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
9 int 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);
20 tm.tv_sec += 5;
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
32 }
33
34
35 pthread_mq_t myq;
36
37 void *thread_recv(void *args) {
38 char str[128];
39 while(1) {
40 pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_FOREVER);
41 printf("RECVD: %.6s\t\t(waiting %d)\n", str, pthread_mq_waiting(&myq));
42 //sleep(1);
43 }
44 }
45
46 int main() {
47 main_mumu();
48
49 char tmp[128];
50
51 pthread_mq_init(&myq, 6, 5);
52
53 pthread_t t;
54 pthread_create(&t, NULL, thread_recv, NULL);
55
56 pthread_mq_send_generic(&myq, "AHOJ1", false, NULL);
57 pthread_mq_send_generic(&myq, "AHOJ2", false, NULL);
58 pthread_mq_send_generic(&myq, "AHOJ3", true, NULL);
59 pthread_mq_send_generic(&myq, "AHOJ4", true, NULL);
60 pthread_mq_send_generic(&myq, "AHOJ5", false, NULL);
61 pthread_mq_send_generic(&myq, "AHOJ6", true, NULL);
62
63 while(1) {
64 pthread_mq_send_generic(&myq, "B", false, NULL);
65 pthread_mq_send_generic(&myq, "A", true, NULL);
66 pthread_mq_send_generic(&myq, " A", false, NULL);
67 pthread_mq_send_generic(&myq, " B", false, NULL);
68 sleep(1);
69 }
70
71 pthread_join(t, NULL);
72 }
73
This page took 0.316214 seconds and 4 git commands to generate.