1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
//Written for Ubuntun 9.10 using g++ in Netbeans 6.5.1
Header file:
class threader {
public:
threader();
threader(const threader& orig);
virtual ~threader();
int createThread(MyNameSpace::Puzzle *mz1;
private:
pthread_t thread, thread2, thread3, thread4, thread5, timerThread;
};
#include statements, some constructors not included. Too many.
MyNameSpace::Puzzle *mz; //some global variables required as a shared resource //for threads
threader::threader() {
}
bool hitEnd = false;
void *maze_thread (void *arg)
{
//HUGE CHUNK OF CODES I TOOK OUT. Did what the threader is supposed to do.
//Did not seem to have a problem after doing some rather extensive debugging.
}
int threader::createThread(MyNameSpace::Puzzle *mz1)
{
int status, status2, status3, statusTime;
mz = (MyNameSpace::Puzzle*)malloc (sizeof (MyNameSpace::Puzzle));
mz = mz1;
void* statusJoin;
void* statusJoin2;
void* statusJoin3;
void* statusJoin4;
void* statusJoin5;
VectorOfVectorOfPointStructType allPaths;
char* name1 = "ThreadOne";
char* name2 = "ThreadTwo";
char* name3 = "ThreadThree";
char* name4 = "ThreadFour";
char* name5 = "ThreadFive";
char* name6 = "ThreadThree";
createCurrMaze();
int i = 0;
bool cont = true;
while (cont == true)
{
//std::cout << "hehe6\n";
status = pthread_create (&thread, NULL, maze_thread, name1);
status2 = pthread_create (&thread2, NULL, maze_thread, name2);
status3 = pthread_create (&thread3, NULL, maze_thread, name3);
status3 = pthread_create (&thread4, NULL, maze_thread, name4);
status3 = pthread_create (&thread5, NULL, maze_thread, name5);
//status = pthread_create (&thread, NULL, maze_thread, NULL);
//pthread_join(thread4, &statusJoin);
//pthread_join(thread5, &statusJoin);
//std::cout << "hehe4\n";
pthread_join(thread, &statusJoin);
//std::cout << "hehe5\n"; //such cout << hehe lines are for debugging.
pthread_join(thread2, &statusJoin2);
pthread_join(thread3, &statusJoin3);
pthread_join(thread4, &statusJoin4);
pthread_join(thread5, &statusJoin5);
i++;
if (hitEnd == true) //a global var. maze_thread sets to true after //solving puzzle.
{
std::cout << "End found!\n";
cont = false;
break;
}
}
}
|
Hi, referring to codes above, all codes pasted lie in Threader class. Only createThread() function belong to threader namespace, the rest are outside of the namespace. I could not get the maze_thread and all other threads it wants to call to belong to a namespace, hence I took them all out of the namespace and header files. My apologies that I cannot include the full code as it is really long with over 600 lines of codes. Not the best of designs.
Basically, main() will call createThread(mz1) to start threading. createCurrMaze(); is a function to initialize the initial puzzle, you know reading in files and the like. This function does not seem to have any problem and hence I did not include it in.
createThread will then create a total of 5 threads which will act independently and run around the puzzle, trying to solve it.
The problem comes when some of them need to be killed when they hit upon dangerous artifacts in the puzzle. When they are killed, control falls back to createThread() again to rethread the same thread from the beginning. This continues until the end is found.
This process of killing and rethreading is when I might get a segmentation fault. The seg fault usually pops up after the threads had already ran through quite a few times, sometimes a few thousands before it occur. I "killed" the thread by letting maze_thread (void *arg) function end its while loop and then returning a NULL, and createThread(MyNameSpace::Puzzle *mz1) will rethread again.
As you noticed, I dropped some numbered std::cout << "hehe" statements throughout the program so as to know where it got stuck at for the seg fault. This is rather rudimentary debugging but the threads will restart thousands of times and hence I cannot use the debugger to loop through it thousand times.
It seems to me the problem mostly lie in createThread(mz1), in the while loop. As to the exact line, I am not too sure as it never seemed to be the same.
It also seem strange to me, that when I took out all the std::cout << "hehe", the seg fault seem to occur much less frequently. When I overload the program with chunks of cout, it seem to crash nearly everytime. When I took them out, it seem much better, probably crashing once every 5 times.
Is there anything I am missing in my threading? I suspect there is something to do with pointers passed to the threads getting mixed up and becoming uninitialised.
Please assist. I am really at my wits end.
Thank you.