わかりました、プログラムで壁にぶつかりました。pthreads を使用して並列プログラムを実装していますが、このブロックで「セグメンテーション違反」が発生しました。
//This is all in the main function
//{{{{{
//Creates the array that holds pointers to bodies
Body **bodies = new Body*[NUMBER_OF_BODIES];
int bodiesperthread = NUMBER_OF_BODIES / NUMBER_OF_THREADS;
//Creates an array to hold the pthreads
pthread_t threads[NUMBER_OF_THREADS];
//Here I partition the array of bodies to each thread
for(int j = 0; j < NUMBER_OF_THREADS; j++)
{
if(j == (NUMBER_OF_THREADS - 1))
{
//Create a new array to hold some bodies
Body **subbodies = new Body*[bodiesremaining];
// Insert the bodies into the new sub array.
for(int i = nextindex; i < bodiesremaining; i++)
subbodies[i] = bodies[i];
// Launch the thread given this sub array.
pthread_create(&threads[j], NULL, &Tree::updateHelp, (void*)subbodies);
}
else
{
// If this is not the last thread we create the sub array with length equal to the
// bodies per thread. Then we update bodies remaining.
Body **subbodies = new Body*[bodiesperthread];
bodiesremaining -= bodiesperthread;
// Populate the new array.
for(int i = nextindex; i < bodiesperthread; i++)
subbodies[i] = bodies[i];
nextindex += bodiesperthread + 1;
// Launch the thread given the sub array.
pthread_create(&threads[j], NULL, &Tree::updateHelp, (void*)subbodies);
}
}
for(int i = 0; i < NUMBER_OF_THREADS; i++)
pthread_join(threads[i], NULL);
//}}}}
//This is in Tree.cpp
void* Tree::updateHelp(void *bodies)
{
long tid = pthread_self();
Body **subbodies = (Body**)bodies;
//According to the CodeBlocks debugger the fault is occuring here
cout << "Thread " << tid << " has a body at (" << subbodies[0]->x << "," << subbodies[0]->y << ") with mass = " << subbodies[0]->mass << ".\n";
pthread_exit(NULL);
}
私はC ++にかなり慣れていて、一生このエラーが発生する理由を理解できません。スレッドをmallocする必要があるからですか?すべてのボディポインターと関係がありますか? サブアレイ用に新しいボディを作成する必要がありますか?
事前にご協力いただきありがとうございます。