linux - 并行计算中的pthreading

标签 linux command pthreads

#include<stdio.h>
#include<math.h>
#include<pthread.h>
#include<stdlib.h> 

long double x,fact[150],pwr[150],s[1];
int i,term;

void *Power(void *temp)
{   
    int k;
    for(k=0;k<150;k++)
    {
        pwr[k] = pow(x,k);
    //printf("%.2Lf\n",pwr[k]);
    } 

    return pwr;
}

void *Fact(void *temp)
{
    long double f;
    int j;

    fact[0] = 1.0;

    for(term=1;term<150;term++)
    {
        f = 1.0;
        for(j=term;j>0;j--)
            f = f * j;

        fact[term] = f;
        //printf("%.2Lf\n",fact[term]);
    }

   return fact;
}

void *Exp(void *temp)
{
    int t;

    s[0] = 0;   
    for(t=0;t<150;t++)
        s[0] = s[0] +  (pwr[t] / fact[t]);

    return s;
}

int main(void)
{
    pthread_t thread1,thread2,thread3;  

    printf("Enter the value of x (between 0 to 100) (for calculating exp(x)) : ");
    scanf("%Lf",&x);

    printf("\nThreads creating.....\n");
    pthread_create(&thread1,NULL,Power,NULL); //calling power function
    pthread_create(&thread2,NULL,Fact,NULL);  //calling factorial function
    printf("Threads created\n");

    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);
    printf("Master thread and terminated threads are joining\n");

    printf("Result collected in Master thread\n");

    pthread_create(&thread3,NULL,Exp,NULL);
    pthread_join(thread3,NULL);

    printf("\nValue of exp(%.2Lf) is : %Lf\n\n",x,s[0]);
    exit(1);
}

我试图在linux ubuntu上运行上面的程序。它给出以下错误

parallelcomp.cpp:(.text+0x1ec): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x207): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x222): undefined reference to `pthread_join'
parallelcomp.cpp:(.text+0x233): undefined reference to `pthread_join'
parallelcomp.cpp:(.text+0x262): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x273): undefined reference to `pthread_join'

该错误很可能是由于将二进制文件与 pthreads 链接所致。

ubuntu终端中有没有命令可以解决这个问题? 我尝试过这个社区论坛中给出的几个命令,但没有一个有帮助。 有人愿意帮助我吗? 我对 Linux ubuntu 也很陌生。 任何形式的建议都是值得赞赏的。 如何包含 libpthread ?

当我在终端中输入以下命令 gcc -pthread -o term term.c 时,我收到以下错误:命令行选项“p”[来自 -pthread] 未知。

最佳答案

请尝试以下-lpthread。您使用的是哪个版本的 gcc?

Why do I get "undefined reference" errors even when I include the right header files?

关于linux - 并行计算中的pthreading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23491560/

相关文章:

ruby 系统命令检查退出代码

php - echo 逐行执行命令的结果

c - 如何判断哪个pthread唤醒了pthread? C

计算C中线程的运行时间

c++ - 了解不同处理器之间的操作系统调度

linux - 我如何在 linux 上制作一个 zip 文件?

linux - 允许非特权用户运行特定命令

Linux 套接字 : Zero-copy local, TCP/IP 远程

c# - 更新大于 1GB 的文件?

linux - 三 | linux终端中的命令