GroupCache::GroupCache (Layer3 * l3, Trace * t) { TRACEPRINTF (t, 4, this, "GroupCacheInit"); this->t = t; this->layer3 = l3; this->enable = 0; pos = 0; memset (updates, 0, sizeof (updates)); pth_mutex_init (&mutex); pth_cond_init (&cond); }
/* Create a condition variable */ SDL_cond * SDL_CreateCond(void) { SDL_cond *cond; cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond)); if ( cond ) { if ( pth_cond_init(&(cond->condpth_p)) < 0 ) { SDL_SetError("pthread_cond_init() failed"); SDL_free(cond); cond = NULL; } } else { SDL_OutOfMemory(); } return(cond); }
pth_cond_t * pth_condi_init (void) { pth_cond_t *c = pth_cond_new (); if (c != (pth_cond_t *)NULL) { if ((pth_condattr_init (c)) == CAF_OK) { if ((pth_cond_init (c)) == CAF_OK) { return c; } else { if ((pth_cond_destroy (c)) == CAF_OK) { pth_cond_delete (c); c = (pth_cond_t *)NULL; } } } else { pth_cond_delete (c); c = (pth_cond_t *)NULL; } } return c; }
int ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond ) { return( pth_cond_init( cond ) ? 0 : errno ); }
int main(int argc, char *argv[]) { if (argc < 5) { return 1; } auto processNum = 4; auto threadNum = atoi(argv[4]); auto dtime = atol(argv[3]) * 1000; auto isPth = std::string(argv[2]) == "pth"; auto taskNum = atoi(argv[1]); long cycles = 0; switch(taskNum) { case 1: task = task1; break; case 2: task = task2; break; case 3: task = task3; break; } time_start(); for (auto i = 0; i < processNum; ++i) { if (fork() != 0) { continue; } if (isPth) { pth_init(); pth_attr_t attr = pth_attr_new(); pth_attr_set(attr, PTH_ATTR_NAME, "task"); pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 64*1024); pth_attr_set(attr, PTH_ATTR_JOINABLE, true); pth_mutex_t mutex; pth_mutex_init(&mutex); pth_cond_init(&pthCond); while (time_stop() < dtime) { for (auto i = workingNum; i < threadNum; ++i) { ++workingNum; pth_spawn(attr, task, &isPth); } int rc; if ((rc = pth_mutex_acquire(&mutex, FALSE, NULL)) != 0) { std::cout << "pthread_mutex_lock " << rc << " " << strerror(rc) << std::endl; return 3; } if (workingNum == threadNum) { if ((rc = pth_cond_await(&pthCond, &mutex, NULL)) != 0) { std::cout << "pthread_cond_wait " << rc << " " << strerror(rc) << std::endl; return 3; } } if ((rc = pth_mutex_release(&mutex)) != 0) { std::cout << "pthread_mutex_unlock " << rc << " " << strerror(rc) << std::endl; return 3; } cycles += threadNum - workingNum; } } else { pthread_attr_t attr; pthread_attr_setstacksize(&attr, 64*1024); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutexattr_t mutexattr; pthread_mutex_init(&mutex, &mutexattr); pthread_condattr_t condattr; pthread_cond_init(&cond, &condattr); pthread_t pid; while (time_stop() < dtime) { for (auto i = workingNum; i < threadNum; ++i) { ++workingNum; if (pthread_create(&pid, NULL, task, &isPth) == -1) { return 2; } } int rc; if ((rc = pthread_mutex_lock(&mutex)) != 0) { std::cout << "pthread_mutex_lock " << rc << " " << strerror(rc) << std::endl; return 3; } if (workingNum == threadNum) { if ((rc = pthread_cond_wait(&cond, &mutex)) != 0) { std::cout << "pthread_cond_wait " << rc << " " << strerror(rc) << std::endl; return 3; } } if ((rc = pthread_mutex_unlock(&mutex)) != 0) { std::cout << "pthread_mutex_unlock " << rc << " " << strerror(rc) << std::endl; return 3; } //pthread_join(pids.front(), NULL); //pids.pop_front(); cycles += threadNum - workingNum; } } std::cout << cycles << std::endl; return 0; } for (auto i = 0; i < processNum; ++i) { wait(NULL); } return 0; }
FrSimThreadCondVar::FrSimThreadCondVar() : isLocked(false) { pth_cond_init(&cond); pth_mutex_init(&mutex); }
int main(int argc, char *argv[]) { int i; sigset_t ss; int sig; pth_event_t ev; /* initialize Pth library */ pth_init(); /* display test program header */ printf("This is TEST_PHILO, a Pth test showing the Five Dining Philosophers\n"); printf("\n"); printf("This is a demonstration showing the famous concurrency problem of the\n"); printf("Five Dining Philosophers as analysed 1965 by E.W.Dijkstra:\n"); printf("\n"); printf("Five philosophers are sitting around a round table, each with a bowl of\n"); printf("Chinese food in front of him. Between periods of talking they may start\n"); printf("eating whenever they want to, with their bowls being filled frequently.\n"); printf("But there are only five chopsticks available, one each to the left of\n"); printf("each bowl - and for eating Chinese food one needs two chopsticks. When\n"); printf("a philosopher wants to start eating, he must pick up the chopstick to\n"); printf("the left of his bowl and the chopstick to the right of his bowl. He\n"); printf("may find, however, that either one (or even both) of the chopsticks is\n"); printf("unavailable as it is being used by another philosopher sitting on his\n"); printf("right or left, so he has to wait.\n"); printf("\n"); printf("This situation shows classical contention under concurrency (the\n"); printf("philosophers want to grab the chopsticks) and the possibility of a\n"); printf("deadlock (all philosophers wait that the chopstick to their left becomes\n"); printf("available).\n"); printf("\n"); printf("The demonstration runs max. 60 seconds. To stop before, press CTRL-C.\n"); printf("\n"); printf("+----P1----+----P2----+----P3----+----P4----+----P5----+\n"); /* initialize the control table */ tab = (table *)malloc(sizeof(table)); if (!pth_mutex_init(&(tab->mutex))) { perror("pth_mutex_init"); exit(1); } for (i = 0; i < PHILNUM; i++) { (tab->self)[i] = i; (tab->status)[i] = thinking; if (!pth_cond_init(&((tab->condition)[i]))) { perror("pth_cond_init"); exit(1); } } /* spawn the philosopher threads */ for (i = 0; i < PHILNUM; i++) { if (((tab->tid)[i] = pth_spawn(PTH_ATTR_DEFAULT, philosopher, &((tab->self)[i]))) == NULL) { perror("pth_spawn"); exit(1); } } /* wait until 60 seconds have elapsed or CTRL-C was pressed */ sigemptyset(&ss); sigaddset(&ss, SIGINT); ev = pth_event(PTH_EVENT_TIME, pth_timeout(60,0)); pth_sigwait_ev(&ss, &sig, ev); pth_event_free(ev, PTH_FREE_ALL); /* cancel and join the philosopher threads */ for (i = 0; i < PHILNUM; i++) pth_cancel((tab->tid)[i]); while (pth_join(NULL, NULL)); /* finish display */ printf("+----------+----------+----------+----------+----------+\n"); /* free the control table */ free(tab); /* shutdown Pth library */ pth_kill(); return 0; }