第二个线程没有打印 - 使用互斥锁进行pthreads

时间:2015-06-07 11:18:18

标签: c multithreading pthreads

我写道:

#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;
}

我得到的照片如下:

~/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$ 

未显示其他线程的打印。

2 个答案:

答案 0 :(得分:3)

第一个线程将globalVarX递增到1000,第二个线程无关。

我建议:

  • 锁定一个增量而不是整个循环。

  • 通过调用给另一个线程增加的机会 sched_yield(),因为如果一个帖子在其时间片中递增globalVarX1000,那么第二个帖子中仍然没有打印。

#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;
}

答案 1 :(得分:2)

从概念上讲,两个线程都有机会增加计数器使用它:

伪代码:

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);
}