C++ (Cpp) pthread_mutex_lock Examples

C++ (Cpp) pthread_mutex_lock - 30 examples found. These are the top rated real world C++ (Cpp) examples of pthread_mutex_lock extracted from open source projects. You can rate examples to help us improve the quality of examples.
void santa() {

    while (1) {

        pthread_mutex_lock(&santa_duerme);
        printf("santa duerme\n");

        while ((sala_espera < MAX_RENOS) && (elfos_dudosos < MAX_ELFOS)) {
            pthread_cond_wait(&santa_libre, &santa_duerme);
        }
        printf("santa se despierta!\n");
        pthread_mutex_lock(&elfos_dudosos_m);
        pthread_mutex_lock(&sala_espera_m);
        if (sala_espera >= MAX_RENOS) {
            pthread_mutex_unlock(&elfos_dudosos_m);
            pthread_mutex_unlock(&sala_espera_m);
            repartir_regalos();
        } else if (elfos_dudosos >= MAX_ELFOS) {
            pthread_mutex_unlock(&elfos_dudosos_m);
            pthread_mutex_unlock(&sala_espera_m);
            resolver_dudas();
        }
        pthread_mutex_unlock(&santa_duerme);
    }
}
Example #2
0
/*!
  Signal the event.
  The behaviour of this message is influenced by the broadcast flag, if this
  is a broadcast event then the event will be signaled to all the waiting
  threads otherwise only one thread will be signaled.
  \param id The calling thread id.
 */
int Event::signal (u_long id)
{
    if (!initialized)
        return -1;

#ifdef HAVE_PTHREAD
    if (broadcast)
    {
        pthread_mutex_lock (&mutex);
        pthread_cond_broadcast (&event);
        pthread_mutex_unlock (&mutex);
    }
    else
    {
        pthread_mutex_lock (&mutex);
        pthread_cond_signal (&event);
        pthread_mutex_unlock (&mutex);
    }
#else
    SetEvent (event);

    /* Reset the event for later reusability.  */
    ResetEvent (event);

#endif
    return 0;
}
Example #3
0
File: 1-2.c Project: Nan619/ltp
/***** Threads functions *****/
void *deadlk_issue(void *arg)
{
    int ret, tmp;

    if ((ret = pthread_mutex_lock(p_mtx))) {
        UNRESOLVED(ret, "First mutex lock in deadlk_issue");
    }
    pthread_cleanup_push(cleanup_deadlk, NULL);
    if ((ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &tmp))) {
        UNRESOLVED(ret, "Set cancel type in deadlk_issue");
    }
    if ((ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &tmp))) {
        UNRESOLVED(ret, "Set cancel state in deadlk_issue");
    }
#if VERBOSE >1
    output("Thread releases the semaphore...\n");
#endif
    if ((ret = sem_post(&semA))) {
        UNRESOLVED(errno, "Sem_post in deadlk_issue");
    }

    returned = 0;
    retval = pthread_mutex_lock(p_mtx);
    returned = 1;

    if ((ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &tmp))) {
        UNRESOLVED(ret, "Set cancel state in deadlk_issue");
    }
    pthread_cleanup_pop(0);
    return NULL;
}
void *llegada_reno(void *param) {

    while (1) {
#ifndef NOSLEEP
        sleep(1 * random() % 5);
#endif
        pthread_mutex_lock(&santa_duerme);
        pthread_mutex_lock(&sala_espera_m);
        if (sala_espera < MAX_RENOS) {
            sala_espera++;
            printf("Llega reno... Total: %d\n", sala_espera);
            if (sala_espera == MAX_RENOS) {
                //Esta la sala llena con todos los renos?
                pthread_mutex_unlock(&sala_espera_m);
                printf("todos renos, repartimos!\n");
                pthread_cond_signal(&santa_libre);
                pthread_cond_wait(&sala_espera_cond, &santa_duerme);
            } else {
                //no? Desbloqueamos variables
                pthread_mutex_unlock(&sala_espera_m);
                pthread_cond_wait(&sala_espera_cond, &santa_duerme);
            }
        } else {
            pthread_mutex_unlock(&sala_espera_m);
        }
        pthread_mutex_unlock(&santa_duerme);
    }
}
Example #5
0
static void *pthread_sleeper(void *arg)
{
    struct cbs_struct *cs;
    cs = arg;

    pthread_mutex_lock(&cs->run_lock);
    while (cs->run != CBS_STATE_DONE)
    {
        pthread_mutex_unlock(&cs->run_lock);

        if (cs->period.tv_sec != 0)
            sleep(cs->period.tv_sec);
        usleep(cs->period.tv_usec);

        pthread_mutex_lock(&cs->run_lock);
        if (cs->run == CBS_STATE_SLEEP)
            cs->run = CBS_STATE_READY;

        pthread_cond_signal(&cs->run_signal);
        pthread_mutex_unlock(&cs->run_lock);
    }

    cs->run = CBS_STATE_DONE;
    pthread_mutex_unlock(&cs->run_lock);

    return NULL;
}
Example #6
0
static void *pthread_wrapper(void *arg)
{
    struct cbs_struct *cs;
    timer_t timer;
    struct sigevent se;
    struct itimerspec its, itsoff;
    double mi_frac;
    cs = arg;

    memset(&se, 0, sizeof(se));
    se.sigev_notify = SIGEV_THREAD_ID;
    se.sigev_signo = SIGXCPU;
    se.sigev_notify_thread_id = syscall(__NR_gettid);
    if (timer_create(CLOCK_MONOTONIC, &se, &timer) != 0)
        abort();

    mi_frac = cs->cpu / BOGO_MIPS;
    memset(&its, 0, sizeof(its));
    if (cs->type == CBS_RT)
    {
        its.it_value.tv_sec = mi_frac;
        its.it_value.tv_nsec = (long)(mi_frac * NANO) % NANO;

        if (its.it_value.tv_sec > cs->period.tv_sec)
            its.it_value.tv_sec = cs->period.tv_sec;
        if (its.it_value.tv_nsec > cs->period.tv_usec * 1000)
            its.it_value.tv_nsec = cs->period.tv_usec * 1000;
    }

    memset(&itsoff, 0, sizeof(itsoff));

    cs->ret = 0;
    while (cs->ret == 0)
    {
        pthread_mutex_lock(&cs->run_lock);
        while (cs->run != CBS_STATE_READY)
            pthread_cond_wait(&cs->run_signal, &cs->run_lock);

        cs->run = CBS_STATE_RUNNING;
        pthread_mutex_unlock(&cs->run_lock);

        if (timer_settime(timer, 0, &its, NULL) != 0)
            abort();

        cs->ret = cs->entry(cs->arg);

        if (timer_settime(timer, 0, &itsoff, NULL) != 0)
            abort();

        pthread_mutex_lock(&cs->run_lock);
        cs->run = CBS_STATE_SLEEP;
        pthread_mutex_unlock(&cs->run_lock);
    }

    pthread_mutex_lock(&cs->run_lock);
    cs->run = CBS_STATE_DONE;
    pthread_mutex_unlock(&cs->run_lock);

    return NULL;
}