| 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 | 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)); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void *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 | |
| 58 | int main() { |
| 59 | //main_mumu(); |
| 60 | |
| 61 | //char tmp[128]; |
| 62 | |
| 63 | pthread_mq_init(&myq, 6, 5); |
| 64 | |
| 65 | pthread_t t; |
| 66 | pthread_create(&t, NULL, thread_recv, NULL); |
| 67 | pthread_t s; |
| 68 | pthread_create(&s, NULL, thread_send, NULL); |
| 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) { |
| 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); |
| 82 | } |
| 83 | |
| 84 | pthread_join(t, NULL); |
| 85 | } |
| 86 | |