891cd504c2632171618254e0f32db5e85bc813d6
[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 += 2;
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 return 0;
33 }
34
35
36 pthread_mq_t myq;
37
38 void *thread_recv() {
39 //(void *)args;
40
41 char str[128];
42 while(1) {
43 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 sleep(1);
46 }
47 }
48
49 int main() {
50 //main_mumu();
51
52 //char tmp[128];
53
54 pthread_mq_init(&myq, 6, 5);
55
56 pthread_t t;
57 pthread_create(&t, NULL, thread_recv, NULL);
58
59 pthread_mq_send_generic(&myq, "AHOJ1", false, NULL);
60 pthread_mq_send_generic(&myq, "AHOJ2", false, NULL);
61 pthread_mq_send_generic(&myq, "AHOJ3", true, NULL);
62 pthread_mq_send_generic(&myq, "AHOJ4", true, NULL);
63 pthread_mq_send_generic(&myq, "AHOJ5", false, NULL);
64 pthread_mq_send_generic(&myq, "AHOJ6", true, NULL);
65
66 while(1) {
67 pthread_mq_send_generic(&myq, "B", false, NULL);
68 pthread_mq_send_generic(&myq, "A", true, NULL);
69 pthread_mq_send_generic(&myq, " A", false, NULL);
70 pthread_mq_send_generic(&myq, " B", false, NULL);
71 sleep(1);
72 }
73
74 pthread_join(t, NULL);
75 }
76
This page took 0.39964 seconds and 3 git commands to generate.