【发布时间】:2014-03-04 18:31:35
【问题描述】:
EDITS----------------我用 gcc-4.8.1 试过了,还是一样的错误。-------------我正在尝试通过共享库使用 pthread 实现一个简单的矩阵乘法示例。但是当我尝试创建共享库时出现此错误:
g++ -shared -o libMatmul.so matmul.o
collect2: ld terminated with signal 11 [Segmentation fault], core dumped
这是我正在使用的代码: matmul.h:
#ifndef matmul_h__
#define matmul_h__
#define SIZE 10
typedef struct {
int dim;
int slice;
} matThread;
int num_thrd;
int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];
int m[SIZE][SIZE];
extern void init_matrix(int m[SIZE][SIZE]);
extern void print_matrix(int m[SIZE][SIZE]);
extern void* multiply(void* matThread);
#endif
matmul.c:
extern "C"
{
#include <pthread.h>
#include <unistd.h>
}
#include <iostream>
#include "matmul.h"
using namespace std ;
matThread* s=NULL;
// initialize a matrix
void init_matrix(int m[SIZE][SIZE])
{
int i, j, val = 0;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
m[i][j] = val++;
}
void print_matrix(int m[SIZE][SIZE])
{
int i, j;
for (i = 0; i < SIZE; i++) {
cout<<"\n\t|" ;
for (j = 0; j < SIZE; j++)
cout<<m[i][j] ;
cout<<"|";
}
}
// thread function: taking "slice" as its argument
void* multiply(void* param)
{
matThread* s = (matThread*)param; // retrive the slice info
int slice1=s->slice;
int D= s->dim=10;
int from = (slice1 * D)/num_thrd; // note that this 'slicing' works fine
int to = ((slice1+1) * D)/num_thrd; // even if SIZE is not divisible by num_thrd
int i,j,k;
cout<<"computing slice " << slice1<<" from row "<< from<< " to " <<to-1<<endl;
for (i = from; i < to; i++)
{
for (j = 0; j < D; j++)
{
C[i][j] = 0;
for ( k = 0; k < D; k++)
C[i][j] += A[i][k]*B[k][j];
}
}
cout<<" finished slice "<<slice1<<endl;
return NULL;
}
main.c:
extern "C"
{
#include <pthread.h>
#include <unistd.h>
}
#include <iostream>
#include "matmul.h"
using namespace std;
// Size by SIZE matrices
// number of threads
matThread* parm=NULL;
int main(int argc, char* argv[])
{
pthread_t* thread; // pointer to a group of threads
int i;
if (argc!=2)
{
cout<<"Usage:"<< argv[0]<<" number_of_threads"<<endl;
exit(-1);
}
num_thrd = atoi(argv[1]);
init_matrix(A);
init_matrix(B);
thread = (pthread_t*) malloc(num_thrd*sizeof(pthread_t));
matThread *parm = new matThread();
for (i = 0; i < num_thrd; i++)
{
parm->slice=i;
// creates each thread working on its own slice of i
if (pthread_create (&thread[i], NULL, multiply, (void*)parm) != 0)
{
cerr<<"Can't create thread"<<endl;
free(thread);
exit(-1);
}
}
for (i = 1; i < num_thrd; i++)
pthread_join (thread[i], NULL);
cout<<"\n\n";
print_matrix(A);
cout<<"\n\n\t *"<<endl;
print_matrix(B);
cout<<"\n\n\t="<<endl;
print_matrix(C);
cout<<"\n\n";
free(thread);
return 0;
}
我使用的命令是: g++ -c -Wall -fPIC matmul.cpp -o matmul.o 和 g++ -shared -o libMatmul.so matmul.o 代码可能看起来有点不对劲,因为我在结构中传递了 SIZE(dim),而它已经在#define 中,但这就是我希望它实现的方式。它是我正在做的一个更大项目的测试程序。 任何帮助是极大的赞赏!提前致谢。
【问题讨论】:
-
你使用的是什么版本的 GCC?
-
我尝试使用 gcc 4.1,它让我创建了共享库,但无法将它与 main 链接。当我尝试使用 gcc-4.6.3 时,上面说的是错误
-
我认为使用 pthread 时有一些编译器/链接器标志。像 -lpthread 之类的东西。
-
使用 4.1 时,我可以创建共享库。当我执行命令时: g++ -L/dir.../ -Wall -o matmul main.cpp -lMatmul -ldl -lpthread 我得到了 glibc 检测到的错误。这又是某种内存错误映射和分段错误。
-
使用
gcc (Debian 4.4.5-8) 4.4.5至少可以成功创建库,如果使用选项-fPIC创建matmul.o。但是在创建多线程对象时,建议使用选项-pthread。
标签: c++ multithreading segmentation-fault pthreads shared-libraries