我最近开始使用线程,所以对这个东西非常陌生。请不要讨厌.i正在开发的一个程序,该程序将计算素数,直到通过命令行提供的限制N。
我创建了一个包含每个线程的上限和下限的结构,我传递了这个结构的一个指针,并使用这个结构的“.lets”和“num_threads=4”属性来控制流。
似乎有某种problem.if,你们任何人都可以指出错误或告诉我,如果这是正确的事情,我将非常感激:)
geany给出了以下错误:
g++ -Wall -o "prime5000" "prime5000.cpp" (in directory: /home/jarrar/operating systems/threads)
/tmp/ccAHGsIw.o: In function `main':
prime5000.cpp:(.text+0xc9): undefined reference to `pthread_create'
prime5000.cpp:(.text+0xf6): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
Compilation failed.如果从终端使用以下命令执行:
g++ prime5000.cpp -lpthread -o 5prime
./5prime它将提供:
分段故障(核心转储)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define num_thread 4
void* calc(void* param);
struct fml
{
long low;
long high;
};
fml* x;
int main(int argc,char* argv[])
{
pthread_t threadArray[num_thread];
int thread;
for(thread=0;thread<num_thread;thread++)
{
x->low=thread*atoi(argv[1])/num_thread;
x->high=x->low+(thread+1)*atoi(argv[1])/num_thread;
pthread_create(&threadArray[thread],NULL,calc,&x);
}
int i;
for(i=0;i<num_thread;i++)
{
pthread_join(threadArray[i],NULL);
}
return 0;
}
void* calc(void* param)
{
fml* temp=(fml*)param;
long lowerLimit=temp->low;
long upperLimit=temp->high;
int i;
int j;
for(i=lowerLimit;i<upperLimit;i++)
{
for(j=2;j<i;j++)
{ if(i!=1||i!=0)
{
if(!(i%j==0))
{
printf("%d ",i);
}
}
}
}
pthread_exit(NULL);
}发布于 2016-04-19 07:25:40
在尝试-lpthread和-pthread之后,问题仍然存在,并且还尝试传递类的对象而不是指针,但同样的问题sadly.segmentation错误(核心转储)没有提供太多信息
https://stackoverflow.com/questions/36634918
复制相似问题