CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2009
    Posts
    29

    Segmentation Fault (using global objects insicde threads)

    Hello everyone,
    im new to this section multithreading, sorry if this question might be too obvious
    im am facing a problem with my program.
    im using posix threads, on linux.
    i have some objects on a global array of pointers. it a big class with pointers, arrayss and other variables inside.

    the thing is that i am creating one thread per object (so each thread calls the objects methods), i pass the object as parameter and inside the thread i call the object member functions.

    i launch the threads in waves of 4 (because i have quad core), when the first wave is launched i collect them with "join". then i launch another wave of 4 or wathever is left (3, 2, 1, or end the program). Repeat this until all objects are sent to a corresponding thread.

    Code:
    void* pthread_function(void* tree){
    
        Knode* tp = (Knode*) tree;
        tp->generateKTree(); //THIS IS THE FUNCTION THAT CAUSES SEG FAULT ONLY WHEN USING THREADS
        pthread_mutex_lock( &mutexContador );
        contador++;
        printf("Contador = %i    tamaño aprox = %i\n", (int)contador, (int)LFactors.size());
        pthread_mutex_unlock( &mutexContador );
        return NULL;
    }
    then i join those last threads too.

    the problem comes on those last threads, i dont know why they cause segemntation fault when they are doing the same as the other threads.

    i've even tried to launch every thread secuentially by locking at the begining with mutex and unlocking at the end, the result was the same segemntation fault.

    inside my code, inside tp->generateKTree(); the seg fault is casued "around" here
    Code:
            printf("debug comment---SEGMENTATION FAULT at next line\n");
            this->reduced = new Knode( vp1Cut(vp1e), this->vexp*(1+v), this->pexp );
            printf("it does not get here\n");
    it does not seg fault on an specific line it is just "around there", it variates.
    sometimes inside the constructor, sometimes it cannot even start the constructor.

    my question.. is there any king of situation where threads can seg fault coming the error from the posix library ??, if not then i can be sure that the fault is somehow in my code.

    but i dont understand why it still crashed when i launched the threads secuentially with a mutex on the whole thread function. it should be equivalent.

    any help is welcome, i know that you guys cannot provide me the solution because the code i posted is poor, but the application is a little big so i better not flood this post.

    any directions on what to do next would be appreciated.
    best regards
    Cristobal
    Last edited by chire; June 10th, 2010 at 01:51 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    Be'er Sheva, Israel
    Posts
    69

    Re: Segmentation Fault (using global objects insicde threads)

    Hi,
    Can you post the complete code, and, do you use -lpthread flag when you compile?
    Ishay Peled
    --How often do you look at a man's shoes?

  3. #3
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Segmentation Fault (using global objects insicde threads)

    It's hard to give a suggestion without seeing complete code.
    Segmentation fault usually caused by refering to invalid memory area. Check the pointers you are using where error comes.
    henky
    ----------------------------------
    [email protected] is not my email address anymore...
    Jangan Pernah Menyerah

  4. #4
    Join Date
    Nov 2009
    Posts
    29

    Re: Segmentation Fault (using global objects insicde threads)

    thanks for the suggestions,

    i found what was the problem.

    the symbolic math library i was using ("ginac") is not thread safe, i contacted them and they told me that probably segmentation faults would happen.

    i confirmed that because when i removed lines of code related to ginac, all worked.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured