gpt4 book ai didi

linux - 并行计算中的pthreading

转载 作者:太空宇宙 更新时间:2023-11-04 03:59:27 25 4
gpt4 key购买 nike

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com