【问题标题】:pthread mutex does not work correctly on macOSpthread 互斥锁在 macOS 上无法正常工作
【发布时间】:2017-05-06 12:55:42
【问题描述】:

目前我正在 Linux 上学习 POSIX 线程。以下示例计算整数数组中有多少个 3(int),在 CentOS 6.5 上返回正确答案,但在 macOS 10.12.4 上返回错误答案。

#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

#define thread_num 16
#define MB 1024 * 1024
int *array;
int length; //array length
int count;
int t; //number of thread
void *count3s_thread(void* id);

pthread_mutex_t myMutex;//create a mutex

int main()
{
    //initialize mutex
    if (pthread_mutex_init(&myMutex, NULL) != 0)
        printf("Mutex init failed!\n");
    int i;
    int tid[thread_num];
    pthread_t threads[thread_num];
    length = 64 * MB;
    array = malloc(length * 4);

    //initialize the array
    //when i is an odd number, array[i] = 4
    //when i is an even number, array[i] = 3
    for (i = 0; i < length; i++)
        array[i] = i % 2 ? 4 : 3;

    for (t = 0; t < thread_num; t++)
    {
        count = 0;
        tid[t]=t;
        int err = pthread_create(&(threads[t]), NULL, count3s_thread,&(tid[t]) );
        if (err)
        {
            printf("create thread error!\n");
            return 0;           
        }
    }
    for (t = 1; t < thread_num; t++)
        pthread_join(threads[t], NULL);
    printf("Total count= %d \n",count);

    pthread_mutex_destroy(&myMutex);

    return 0;
}

void *count3s_thread(void* id)
{
    //printf("id from count3s_thread = %d\n", *(int *)id);
    int length_per_thread = length / thread_num; //length of every thread
    int start = *(int *)id * length_per_thread;
    int i;
    for (i = start; i < start + length_per_thread; i++)
    {
        if (array[i] == 3)
        {
            pthread_mutex_lock(&myMutex);
            count++;
            pthread_mutex_unlock(&myMutex);
        }
    }
    return NULL;
}

答案应该是 64 * 1024 * 1024 / 2 = 67,108,864 / 2 = 33,554,432。但 macOS 上的答案永远不会正确。我使用以下选项编译代码:

gcc mutex-demo.c -lpthread -o mutex-demo

我已经尝试解决this link 之后的问题。但是,在初始化 pthread 互斥锁后,我仍然得到了错误的答案。我错过了什么吗?

PS:我尝试了pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_init( &amp;mutex, NULL),但在 macOS 上运行的程序仍然返回错误的答案。

高级感谢!

【问题讨论】:

  • for 循环为 pthread_join 开始于 1 而不是 0
  • 如果你在没有pthread_create的情况下调用函数,你得到正确的值了吗?我没有得到你的期望值:ideone.com/z3Ne2a

标签: c linux macos pthreads mutex


【解决方案1】:

pthread_create 之前的行count = 0;count 设置为0,因此在线程递增它们的同时重置count 的值。删除此行。它可能适用于其他系统,当主线程在线程开始计数之前完成pthread_create

我已经在 cmets 中提到过:

for (t = 1; t < thread_num; t++)
    pthread_join(threads[t], NULL);

应该是

for (t = 0; t < thread_num; t++)
     //  ^
    pthread_join(threads[t], NULL);

否则你不会等待第一个线程完成。

【讨论】:

  • 谢谢!在我删除行count = 0; 并在pthread_join 循环中设置t = 0 后,结果终于正确了。但是,我还有一个问题:为什么pthread_create 会自动将count 设置为0?函数pthread_createcount3s_thread 都没有可以将count 设置为0 的语句。
  • count 是一个全局变量,0 已初始化。
  • 哦,我忘了C中的全局变量会自动初始化为0。不管怎样,谢谢你告诉我这么多。
猜你喜欢
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 2012-06-05
  • 2013-06-06
  • 1970-01-01
  • 2021-09-23
相关资源
最近更新 更多