本文整理汇总了C++中cond_init函数的典型用法代码示例。如果您正苦于以下问题:C++ cond_init函数的具体用法?C++ cond_init怎么用?C++ cond_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cond_init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
/**
* Creates a case in which two threads will be racing to write one variable. Exected
* x will not be equal to actual x for a large enough loop unless there are locks.
*/
int main(int argc, char *argv[])
{
//Create 2 new threads.
if(argc != 3){
printf(2, "Usage: condtest <loop count> <num consumers>\n");
exit();
}
int pid;
loops = atoi(argv[1]);
numconsumers = atoi(argv[2]);
//initialize locks and condition variables
mutex_init(&mutex);
cond_init(&empty);
cond_init(&full);
//create one producer
pid = thread_create(&producer, (void *)"A"); //Thread 1: Add 1 to x
printf(2, "Parent created producer with pid %d.\n", pid);
int i;
//create x number of consumers
for (i = 0; i < numconsumers; i++){
pid = thread_create(&consumer, 0);
mutex_lock(&print);
printf(2, "Created consumer %d with pid %d.\n", i, pid);
mutex_unlock(&print);
}
//wait for children
for(i = 0; i < numconsumers+1; i++){
thread_wait();
}
exit();
}
示例2: cache_init
void
cache_init (void)
{
int i;
for (i = 0; i < CACHE_SIZE; i++)
{
lock_init (&block_cache[i].lock);
cond_init (&block_cache[i].r_w_done);
block_cache[i].old_sector = -1;
block_cache[i].sector = -1;
}
//first block is preallocated for the free map.
// it will not be evicted
block_cache[0].sector = 0;
block_cache[0].in_use = true;
block_cache[0].IO_needed = true;
cache_hand = 0;
lock_init (&cache_lock);
list_init (&read_ahead_queue);
lock_init (&read_ahead_lock);
cond_init (&read_ahead_go);
thread_create ("write-behind", PRI_DEFAULT, write_behind_func, NULL);
thread_create ("read_ahead", PRI_DEFAULT, read_ahead_func, NULL);
}
示例3: rwlock_init
/* readers/writer lock function for SunOS 4.1.x added by H.Nakagaki */
int rwlock_init(rwlock_t *rw, int c, void *d)
{
mutex_init(&(rw->lock), 0, 0);
cond_init(&(rw->r_cond), &(rw->lock));
cond_init(&(rw->w_cond), &(rw->lock));
rw->readers = 0;
}
示例4: init_file_synch
static void init_file_synch (struct file_synch_status *fss) {
lock_init (&fss->lock);
lock_init (&fss->dir_lock);
fss->writers_waiting = 0;
fss->readers_running = 0;
cond_init (&fss->read_cond);
cond_init (&fss->write_cond);
}
示例5: init_producer_consumer
/*
* Initializes the producer and consumer
* monitor variables
*/
void
init_producer_consumer(void) {
/* monitor lock */
lock_init(&mutex);
/* buffer conditions */
cond_init(¬_empty);
cond_init(¬_full);
}
示例6: main
int main(void)
{
report_start(START_CMPLT);
//assuredly_misbehave((rand() % 521) % MISBEHAVE_MAX);
misbehave(BGND_BRWN >> FGND_CYAN); // for landslide
ERR(thr_init(STACK_SIZE));
ERR(mutex_init(&lock1));
ERR(mutex_init(&lock2));
ERR(cond_init(&cvar1));
ERR(cond_init(&cvar2));
ERR(thr_create(thread1, NULL));
report_misc("thread1 created");
/* Wait for thread1 to get to sleep on cvar1. */
mutex_lock(&lock1);
while (!slept1) {
mutex_unlock(&lock1);
thr_yield(-1);
mutex_lock(&lock1);
}
/* Indicate that we are about to signal */
signaled1 = 1;
mutex_unlock(&lock1);
/* Signal. Note that we know for sure that thread1 is asleep on
* cvar1 (assuming correct cond vars and mutexes...) */
cond_signal(&cvar1);
report_misc("cvar1 signaled");
//sleep(10);
/* Now do it all again for the second set of things. */
/* Wait for thread1 to get to sleep on cvar2. */
mutex_lock(&lock2);
while (!slept2) {
mutex_unlock(&lock2);
thr_yield(-1);
mutex_lock(&lock2);
}
/* Indicate that we are about to signal */
signaled2 = 1;
mutex_unlock(&lock2);
/* Signal. Note that we know for sure that thread1 is asleep on
* cvar1 (assuming correct cond vars and mutexes...) */
cond_signal(&cvar2);
report_misc("cvar2 signaled");
/* We report success from the other thread. */
return 0;
}
示例7: reaction_init
void reaction_init(struct reaction *reaction)
{
reaction->new_h_arrival = malloc(sizeof(struct condition));
reaction->reaction_occurred_h = malloc(sizeof(struct condition));
reaction->lck = malloc(sizeof(struct lock));
cond_init(reaction->new_h_arrival);
cond_init(reaction->reaction_occurred_h);
lock_init(reaction->lck);
reaction->h_atoms = 0;
}
示例8: rwlock_init
/** @brief init a rwlock
*
* This function should initialize the lock pointed
* to by rwlock. Effects of using a lock before it has been initialized may be
* undefined. This function returns zero on success and a number less than
* zero on error.
*
* @para rwlock:
* @return error or success
**/
int rwlock_init(rwlock_t *rwlock)
{
rwlock->rd_cnt= 0;
rwlock->wr_cnt= 0;
mutex_init(&rwlock->rdcnt_mutex);
mutex_init(&rwlock->wrcnt_mutex);
cond_init(&rwlock->rd_cond);
cond_init(&rwlock->wr_cond);
cond_init(&rwlock->writer_cond);
return OK;
}
示例9: postfork1_child_tpool
void
postfork1_child_tpool(void)
{
pthread_t my_tid = pthread_self();
tpool_t *tpool;
tpool_job_t *job;
/*
* All of the thread pool workers are gone, except possibly
* for the current thread, if it is a thread pool worker thread.
* Retain the thread pools, but make them all empty. Whatever
* jobs were queued or running belong to the parent process.
*/
top:
if ((tpool = thread_pools) == NULL)
return;
do {
tpool_active_t *activep;
(void) mutex_init(&tpool->tp_mutex, USYNC_THREAD, NULL);
(void) cond_init(&tpool->tp_busycv, USYNC_THREAD, NULL);
(void) cond_init(&tpool->tp_workcv, USYNC_THREAD, NULL);
(void) cond_init(&tpool->tp_waitcv, USYNC_THREAD, NULL);
for (job = tpool->tp_head; job; job = tpool->tp_head) {
tpool->tp_head = job->tpj_next;
lfree(job, sizeof (*job));
}
tpool->tp_tail = NULL;
tpool->tp_njobs = 0;
for (activep = tpool->tp_active; activep;
activep = activep->tpa_next) {
if (activep->tpa_tid == my_tid) {
activep->tpa_next = NULL;
break;
}
}
tpool->tp_idle = 0;
tpool->tp_current = 0;
if ((tpool->tp_active = activep) != NULL)
tpool->tp_current = 1;
tpool->tp_flags &= ~TP_WAIT;
if (tpool->tp_flags & (TP_DESTROY | TP_ABANDON)) {
tpool->tp_flags &= ~TP_DESTROY;
tpool->tp_flags |= TP_ABANDON;
if (tpool->tp_current == 0) {
delete_pool(tpool);
goto top; /* start over */
}
}
} while ((tpool = tpool->tp_forw) != thread_pools);
}
示例10: bounded_buffer_init
void bounded_buffer_init(bounded_buffer_t *b, size_t size) {
b->buffer = (void **)mem_calloc(size, sizeof(void *));
b->size = size;
b->count = 0;
b->head = 0;
b->tail = 0;
mutex_init(&b->mutex);
cond_init(&b->empty);
cond_init(&b->fill);
b->done = 0;
b->workers = 0;
mutex_init(&b->worker_mutex);
}
示例11: o_conInit
o_con_t o_conInit(int dummy) {
o_con_t c;
c = (o_con_t)malloc( sizeof(cond_t) );
cond_init( c, USYNC_THREAD, NULL );
return c;
}
示例12: strlen
Thread::Thread( char *name, void *context, void *(*action)(void *) )
{
m_name = new char[ strlen(name) + 1 ];
strcpy( m_name, name );
m_context = context;
m_action = action;
// lock the registry ... add this thread to the registry
if ( Thread::count == 0 ) {
if ( mutex_init( &thread_mtx, USYNC_THREAD, NULL ) != 0 ) {
printf( "Thread init fails: thread_mutex could not be created.");
}
if ( mutex_init( &barrier_mtx, USYNC_THREAD, NULL ) != 0 ) {
printf( "Thread init fails: barrier_mutex could not be created.");
}
if ( cond_init( &barrier_cv, USYNC_THREAD, NULL ) != 0 ) {
printf( "Thread init fails: condition var could not be created.");
}
barrier_count = 0;
}
if ( mutex_lock( &thread_mtx ) != 0 ) {
printf( "Thread init fails: thread_mutex not allocated" );
}
Thread::thread_count++;
m_id = Thread::count++;
Thread::registry[m_id] = this;
if ( mutex_unlock( &thread_mtx ) != 0 ) {
printf( "Thread init fails: thread_mutex could not unlock ");
}
m_procid = (processorid_t) -1;
}
示例13: alloc_group
/* allocate and initialize group */
static vntsd_group_t *
alloc_group(vntsd_t *vntsdp, char *group_name, uint64_t tcp_port)
{
vntsd_group_t *groupp;
/* allocate group */
groupp = (vntsd_group_t *)malloc(sizeof (vntsd_group_t));
if (groupp == NULL) {
vntsd_log(VNTSD_ERR_NO_MEM, "alloc_group");
return (NULL);
}
/* initialize group */
bzero(groupp, sizeof (vntsd_group_t));
(void) mutex_init(&groupp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL);
(void) cond_init(&groupp->cvp, USYNC_THREAD, NULL);
if (group_name != NULL) {
(void) memcpy(groupp->group_name, group_name, MAXPATHLEN);
}
groupp->tcp_port = tcp_port;
groupp->listen_tid = (thread_t)-1;
groupp->sockfd = (thread_t)-1;
groupp->vntsd = vntsdp;
D1(stderr, "[email protected]%d [email protected]%lld:%s\n", thr_self(), groupp->tcp_port,
groupp->group_name);
return (groupp);
}
示例14: worker_init
/*===========================================================================*
* worker_init *
*===========================================================================*/
PUBLIC void worker_init(struct worker_thread *wp)
{
/* Initialize worker thread */
if (!init) {
threads_init();
if (mthread_attr_init(&tattr) != 0)
panic("failed to initialize attribute");
if (mthread_attr_setstacksize(&tattr, TH_STACKSIZE) != 0)
panic("couldn't set default thread stack size");
if (mthread_attr_setdetachstate(&tattr, MTHREAD_CREATE_DETACHED) != 0)
panic("couldn't set default thread detach state");
pending = 0;
init = 1;
}
ASSERTW(wp);
wp->w_job.j_func = NULL; /* Mark not in use */
wp->w_next = NULL;
if (mutex_init(&wp->w_event_mutex, NULL) != 0)
panic("failed to initialize mutex");
if (cond_init(&wp->w_event, NULL) != 0)
panic("failed to initialize conditional variable");
if (mthread_create(&wp->w_tid, &tattr, worker_main, (void *) wp) != 0)
panic("unable to start thread");
yield();
}
示例15: nscd_wait
int
nscd_wait(nsc_ctx_t *ctx, nsc_db_t *nscdb, nsc_entry_t *entry)
{
waiter_t mywait;
waiter_t *wchan = &nscdb->db_wait;
(void) cond_init(&(mywait.w_waitcv), USYNC_THREAD, 0);
mywait.w_key = entry;
mywait.w_signaled = 0;
mywait.w_next = wchan->w_next;
mywait.w_prev = wchan;
if (mywait.w_next)
mywait.w_next->w_prev = &mywait;
wchan->w_next = &mywait;
(void) mutex_lock(&ctx->stats_mutex);
ctx->stats.wait_count++;
(void) mutex_unlock(&ctx->stats_mutex);
while (!mywait.w_signaled)
(void) cond_wait(&(mywait.w_waitcv), &nscdb->db_mutex);
if (mywait.w_prev)
mywait.w_prev->w_next = mywait.w_next;
if (mywait.w_next)
mywait.w_next->w_prev = mywait.w_prev;
return (0);
}