为什么2个线程的执行比C中的1个线程慢? [英] Why is the execution of 2 threads slower than that of 1 thread in C?

查看:56
本文介绍了为什么2个线程的执行比C中的1个线程慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pthread 库制作一个程序,用莱布尼茨公式找到 pi 的准确值.我正在这里处理共享资源.我的多线程函数如下所示:

I am using the pthread library to make a program to find the accurate value of pi with Leibniz formula. I am working on shared resource here. My multithreaded function looks like this:

void *Leibniz(void *vars)
{
    struct variables *val = (struct variables *)vars;
    int startLimit = val->start;
    int endLimit = val->end;
    
    int i;
    for (i = startLimit; i <= endLimit; i++)
    {
        pthread_mutex_lock(&mutex);
        sum += (pow(-1, i) / ((2 * i) + 1));
        pthread_mutex_unlock(&mutex);
    }
}

当我用 N 次迭代和 1 个线程运行程序时,我平均在大约 4.5 秒内得到正确的输出.当我用两个线程运行同一个程序时,大约需要 18 秒.我必须使用多线程来使程序更快,但恰恰相反.谁能解释一下原因?

When I run the program with N iterations and 1 thread, I get the correct output in about 4.5 seconds average. When I run the same program with two threads, it takes around 18 seconds. I have to use multithreading to make the program faster but the exact opposite is happening. Can anyone explain why?

推荐答案

看起来你没有表达你的想法.

It looks like you aren't expressing what you thought.

您可能想要的是在计算结束时更新总和,而不是锁定每个循环迭代(这会因多次上下文切换而降低性能):

Instead of locking on each loop iteration (which degrades performance due to many context switches), what you probably wanted is updating the sum at the end of calculation:

(注意:更新共享和时只需要1个锁):

(note: only 1 lock needed when updating the shared sum):

{
    struct variables *val = (struct variables *)vars;
    int startLimit = val->start;
    int endLimit = val->end;

    // note: this part is calculated by each thread separatly
    // no (expensive) locking here

    double local_sum = 0;
    for (int i = startLimit; i <= endLimit; i++)
    {
         local_sum += (pow(-1, i) / ((2 * i) + 1));
    }

    // now update the sum (which is shared among all threads)
    // so need some kind of synchronization here
    // -> only 1 lock instead of (endLimit - startLimit + 1) times locking
    pthread_mutex_lock(&mutex);
    sum += local_sum;
    pthread_mutex_unlock(&mutex);
}

这篇关于为什么2个线程的执行比C中的1个线程慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆