X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Ftest.c;h=f39a979cb7ade2f6acc46ce9e1e79f4042967103;hb=24286ba9a7ae3a8c6f5eefe9455e18bd758e742b;hp=a41ca54a0023cdcfc714f18540d5fc9fe30ec4da;hpb=3aac261937c3a94461f4dd8fa64c17d5e158c692;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/test.c b/c/pthread_extra/test.c index a41ca54..f39a979 100644 --- a/c/pthread_extra/test.c +++ b/c/pthread_extra/test.c @@ -17,7 +17,7 @@ int main_mumu() { //Set timeout struct timespec tm; clock_gettime(CLOCK_REALTIME, &tm); - tm.tv_sec += 5; + tm.tv_sec += 2; //Lock one of the locks for testing pthread_mutex_lock(lck[2]); @@ -29,29 +29,43 @@ int main_mumu() { printf("FAILED!\n"); } + return 0; } pthread_mq_t myq; -void *thread_recv(void *args) { +void *thread_recv() { + //(void *)args; + char str[128]; while(1) { - pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_FOREVER); - printf("RECVD: %.6s\t\t(waiting %d)\n", str, pthread_mq_waiting(&myq)); - //sleep(1); + if(pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_FOREVER)) + printf("-RECVD: %.6s\t\t(waiting %d)\n", str, (int)pthread_mq_waiting(&myq)); + if(pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_NOBLOCK)) + printf("+RECVD: %.6s\t\t(waiting %d)\n", str, (int)pthread_mq_waiting(&myq)); + } +} + +void *thread_send() { + while(1) { + pthread_mq_send_generic(&myq, " X ", true, PTHREAD_XTIME_NOBLOCK); + pthread_mq_send_generic(&myq, " Y ", false, PTHREAD_XTIME_FOREVER); } } + int main() { - main_mumu(); + //main_mumu(); - char tmp[128]; + //char tmp[128]; pthread_mq_init(&myq, 6, 5); pthread_t t; pthread_create(&t, NULL, thread_recv, NULL); + pthread_t s; + pthread_create(&s, NULL, thread_send, NULL); pthread_mq_send_generic(&myq, "AHOJ1", false, NULL); pthread_mq_send_generic(&myq, "AHOJ2", false, NULL); @@ -61,11 +75,10 @@ int main() { pthread_mq_send_generic(&myq, "AHOJ6", true, NULL); while(1) { - pthread_mq_send_generic(&myq, "B", false, NULL); - pthread_mq_send_generic(&myq, "A", true, NULL); - pthread_mq_send_generic(&myq, " A", false, NULL); - pthread_mq_send_generic(&myq, " B", false, NULL); - sleep(1); + pthread_mq_send_generic(&myq, "B ", false, NULL); + pthread_mq_send_generic(&myq, "A ", true, NULL); + pthread_mq_send_generic(&myq, " B ", false, PTHREAD_XTIME_FOREVER); + pthread_mq_send_generic(&myq, " A ", false, PTHREAD_XTIME_NOBLOCK); } pthread_join(t, NULL);