简体   繁体   English

从第二个线程不打印-带互斥锁的pthreads

[英]No prints from the second thread - pthreads with mutex locks

I wrote: 我写:

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

int globalVarX;

void *threadFunction (void *arg)
{
    pthread_mutex_lock (&mutex1);

    while (globalVarX < 1000)
    {
        printf("x increment by thread id: %d", gettid ());
        ++globalVarX;
    }

    pthread_mutex_unlock (&mutex1);

    return NULL;
}

int main()
{
    pthread_t threadA;
    pthread_t threadB;

    if (pthread_create (&threadA, NULL, threadFunction, NULL)) 
    {
        fprintf (stderr, "Error creating thread\n");
        return 1;
    }

    if (pthread_create (&threadB, NULL, threadFunction, NULL)) 
    {
        fprintf (stderr, "Error creating thread\n");
        return 1;
    }

    if (pthread_join (threadA, NULL))
    {
        fprintf (stderr, "Error joining thread\n");
        return 2;
    }

    if (pthread_join (threadB, NULL))
    {
        fprintf (stderr, "Error joining thread\n");
        return 2;
    }

    return 0;
}

Prints I am getting are as follows: 我得到的打印如下:

~/studies$ ./a.out 
x increment by thread id: 3154
x increment by thread id: 3154
x increment by thread id: 3154
x increment by thread id: 3154
x increment by thread id: 3154
x increment by thread id: 3154
x increment by thread id: 3154
x increment by thread id: 3154
x increment by thread id: 3154
x increment by thread id: 3154
~/studies$ 

Prints from the other thread are not shown. 未显示来自其他线程的打印。

The first thread increments globalVarX to 1000 and the second thread has nothing to do. 第一个线程将globalVarX递增到1000 ,第二个线程无关。

I suggest: 我建议:

  • Locking one increment instead of the whole loop. 锁定一个增量而不是整个循环。

  • Give the other thread the opportunity to increment also by calling sched_yield() , because if one thread increments globalVarX to 1000 in its time slice there still would be no prints from the second thread. 也可以通过调用sched_yield()来给另一个线程增加增量的机会,因为如果一个线程在其时间片中将globalVarX增加到1000 ,则第二个线程仍然没有打印输出。

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

int globalVarX;

void *threadFunction (void *arg)
{
    int flagbreak = 0;

    for(;!flagbreak;) {
        pthread_mutex_lock (&mutex1);
        if (globalVarX >= 1000) flagbreak = 1;
        else {
            ++globalVarX;
            printf("x increment by thread id: %ld\n", syscall(SYS_gettid));
        }
        pthread_mutex_unlock (&mutex1);
        sched_yield();
    }
    return NULL;
}

int main(void)
{
    pthread_t threadA;
    pthread_t threadB;

    if (pthread_create (&threadA, NULL, threadFunction, NULL)) 
    {
        fprintf (stderr, "Error creating thread\n");
        return 1;
    }

    if (pthread_create (&threadB, NULL, threadFunction, NULL)) 
    {
        fprintf (stderr, "Error creating thread\n");
        return 1;
    }

    if (pthread_join (threadA, NULL))
    {
        fprintf (stderr, "Error joining thread\n");
        return 2;
    }

    if (pthread_join (threadB, NULL))
    {
        fprintf (stderr, "Error joining thread\n");
        return 2;
    }

    return 0;
}

To conceptually give both threads a chance on incrementing the counter use this: 要从概念上给两个线程一个增加计数器的机会,请使用以下命令:

Pseudo code: 伪代码:

Condition cond = condition_init;
Mutex mutex = mutex_init;

int counter = 0;

thread_func
{
  mutex_lock(mutex);

  while (counter < 1000)
  {
    ++counter;
    print thread, counter;
    condition_signal(cond);
    condition_wait(cond, mutex);
  }  

  condition_signal(cond);

  mutex_unlock(mutex);
}

main
{
  Thread thread_a = thread_create(thread_func);
  Thread thread_b = treadd_create(thread_func);

  thread_join(thread_a);
  thread_join(thread_b);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM