static VALUE rb_szqueue_clear(VALUE self) { rb_ary_clear(GET_QUEUE_QUE(self)); wakeup_all_threads(GET_SZQUEUE_WAITERS(self)); return self; }
static VALUE queue_do_close(VALUE self, int is_szq) { if (!queue_closed_p(self)) { FL_SET(self, QUEUE_CLOSED); if (queue_num_waiting(self) > 0) { VALUE waiters = GET_QUEUE_WAITERS(self); wakeup_all_threads(waiters); } if (is_szq && szqueue_num_waiting_producer(self) > 0) { VALUE waiters = GET_SZQUEUE_WAITERS(self); wakeup_all_threads(waiters); } } return self; }
/* ========================================================================================== */ void *routine_A(void *) { wait_signal(cond, mutex); //for(int i = 0; i < CYCLE_COUNT; i++) { printf("A"); strcat(result_str, "A"); //QTest::qSleep(SLEEPING_TIME); //usleep(SLEEPING_TIME); } Sleep(1); wakeup_all_threads(wait_A, mutex_A); return NULL; }
void *routine_G(void *) { wait_signal(cond, mutex); wait_signal(wait_E, mutex_E); //for(int i = 0; i < CYCLE_COUNT + 5; i++) { sem_wait(&tSemaphore); strcat(result_str, "G"); printf("G"); sem_post(&tSemaphore); //QTest::qSleep(SLEEPING_TIME); //usleep(SLEEPING_TIME); } //sleep(1); wakeup_all_threads(wait_G, mutex_G); return NULL; }
void *routine_E(void *) { wait_signal(cond, mutex); wait_signal(wait_B, mutex); //for(int i = 0; i < CYCLE_COUNT * 2; i++) { pthread_mutex_lock(&tMutex); printf("E"); strcat(result_str, "E"); pthread_mutex_unlock(&tMutex); //QTest::qSleep(SLEEPING_TIME); //usleep(SLEEPING_TIME); } //sleep(1); wakeup_all_threads(wait_E, mutex_E); return NULL; }
void *routine_F(void *) { wait_signal(cond, mutex); wait_signal(wait_D, mutex_D); //for(int i = 0; i < CYCLE_COUNT * 2; i++) { sem_wait(&tSemaphore); strcat(result_str, "F"); printf("F"); sem_post(&tSemaphore); //QTest::qSleep(SLEEPING_TIME); //usleep(SLEEPING_TIME); } //sleep(1); free_F = true; wakeup_all_threads(wait_F, mutex_F); return NULL; }
/* ========================================================================================== */ int main() { /* ================================================================ */ pthread_attr_t attrs; pthread_attr_init(&attrs); pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_JOINABLE); pthread_mutex_init(&tMutex, NULL); sem_init(&tSemaphore, NULL, MAX_SEM_THREAD_COUNT); free_F = free_H = free_K = free_P = false; /* ================================================================ */ /* if (pthread_create(&Thread_A, &attrs, routine_A, NULL)) printf("[main] error creating thread A.\n"); if (pthread_create(&Thread_B, &attrs, routine_B, NULL)) printf("[main] error creating thread B.\n"); if (pthread_create(&Thread_C, &attrs, routine_C, NULL)) printf("[main] error creating thread C.\n"); if (pthread_create(&Thread_E, &attrs, routine_E, NULL)) printf("[main] error creating thread E.\n"); if (pthread_create(&Thread_D, &attrs, routine_D, NULL)) printf("[main] error creating thread D.\n"); if (pthread_create(&Thread_F, &attrs, routine_F, NULL)) printf("[main] error creating thread F.\n"); if (pthread_create(&Thread_G, &attrs, routine_G, NULL)) printf("[main] error creating thread G.\n"); if (pthread_create(&Thread_H, &attrs, routine_H, NULL)) printf("[main] error creating thread H.\n"); if (pthread_create(&Thread_P, &attrs, routine_P, NULL)) printf("[main] error creating thread P.\n"); if (pthread_create(&Thread_K, &attrs, routine_K, NULL)) printf("[main] error creating thread K.\n"); if (pthread_create(&Thread_N, &attrs, routine_N, NULL)) printf("[main] error creating thread N.\n"); if (pthread_create(&Thread_M, &attrs, routine_M, NULL)) printf("[main] error creating thread M.\n"); /**/ if (pthread_create(&Thread_P, &attrs, routine_P, NULL)) printf("[main] error creating thread P.\n"); if (pthread_create(&Thread_E, &attrs, routine_E, NULL)) printf("[main] error creating thread E.\n"); if (pthread_create(&Thread_H, &attrs, routine_H, NULL)) printf("[main] error creating thread H.\n"); if (pthread_create(&Thread_B, &attrs, routine_B, NULL)) printf("[main] error creating thread B.\n"); if (pthread_create(&Thread_K, &attrs, routine_K, NULL)) printf("[main] error creating thread K.\n"); if (pthread_create(&Thread_C, &attrs, routine_C, NULL)) printf("[main] error creating thread C.\n"); if (pthread_create(&Thread_A, &attrs, routine_A, NULL)) printf("[main] error creating thread A.\n"); if (pthread_create(&Thread_F, &attrs, routine_F, NULL)) printf("[main] error creating thread F.\n"); if (pthread_create(&Thread_M, &attrs, routine_M, NULL)) printf("[main] error creating thread M.\n"); if (pthread_create(&Thread_G, &attrs, routine_G, NULL)) printf("[main] error creating thread G.\n"); if (pthread_create(&Thread_D, &attrs, routine_D, NULL)) printf("[main] error creating thread D.\n"); if (pthread_create(&Thread_N, &attrs, routine_N, NULL)) printf("[main] error creating thread N.\n"); /* ================================================================ */ Sleep(5); wakeup_all_threads(cond, mutex); /* ================================================================ */ view_join_status(Thread_A, "main", "A"); view_join_status(Thread_B, "main", "B"); view_join_status(Thread_C, "main", "C"); view_join_status(Thread_D, "main", "D"); view_join_status(Thread_E, "main", "E"); view_join_status(Thread_F, "main", "F"); view_join_status(Thread_G, "main", "G"); view_join_status(Thread_H, "main", "H"); view_join_status(Thread_K, "main", "K"); view_join_status(Thread_M, "main", "M"); view_join_status(Thread_N, "main", "N"); /* ================================================================ */ strcat(result_str, "\0"); printf("\n\"%s\"\n", result_str); pthread_mutex_destroy(&tMutex); /* ========================================== */ printf("\nHello, world!\n"); _getch(); return 0; }
static VALUE rb_condvar_broadcast(VALUE self) { wakeup_all_threads(GET_CONDVAR_WAITERS(self)); return self; }