Thread: pthread_create problem

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    pthread_create problem

    Hi, I have problem with my code. In Fedora Core 4, I could compile and run it. Then I changed distro to Ubuntu Breezy Badger ( 5.10 ). I can compile it and when I run it, I get segmentation fault.

    Here is the code:
    http://www.akbarhome.com/download/te..._wallpapoz.cpp

    It is 245 lines. You don't have to read all the lines. The problem lies in this section:
    Code:
    	
            //ok, this is the time for the thread to work
    	int thread_param[workspace_total];
    	for( int j=0; j<workspace_total; j++ )
    	{
    	    thread_param[j] = j;
    	    pthread_create( &thread_id[j], NULL, &show_wallpaper, &thread_param[j]);
    	}
    If I comment the pthread_create line, the program runs fine, of course without the additional feature that depend on thread. You must remember that this code runs fine in Fedora Core 4 ( without segmentation fault ). I try test the simple code with pthread_create in Ubuntu distro like this:
    Code:
    #include <iostream>
    using namespace std;
    
    void* show( void* param )
    {
        cout << "testing" << endl;
    }
    
    int main()
    {
        pthread_t thread_id[4];
    
        pthread_create( &thread_id[0], NULL, &show, NULL );
    
        while(true) {}
    
    }
    And it runs fine. I am confuse. Any body can help me? I don't know how to debug thread. The only way I know to debug thread is with printf or cout. It get segmentation fault before it execute the first line of function that thread go in, that is show_wallpaper.

    Thank you.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > int thread_param[workspace_total];
    A variable length array?

    > pthread_create( &thread_id[j], NULL, &show_wallpaper, &thread_param[j]);
    You're passing pointers to local variables - when the variable goes out of scope your thread is using unallocated memory - bye bye thread

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    >A variable length array?
    Yes, it is integer array that size is know at runtime.

    >You're passing pointers to local variables - when the variable goes out of scope your thread is using unallocated memory - bye bye thread
    I don't understant your statement. I try to make it all global variables. I try to change j to number like 0,1,2. Nothing works.

    Looking into this url:
    http://www.yolinux.com/TUTORIALS/Lin...IONTERMINATION
    I try to put this code into my program:
    Code:
    void* show( void* c)
    {
        cout << "testing" << endl;
    }
    This code is outside the main function.

    Then in the beginning of main function, I put this:
    Code:
    int main()
    {
    	    pthread_t thread1;
    	    char* b = "3333";
    	    pthread_create( &thread1, NULL, show, (void *)b);
    	    cout << "bla" << endl;
    The program crash (segmentation fault) before it print "bla" or "testing" to stdout. Any idea?
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    What does pthread_create return?

  5. #5
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Harbinger has it right. You are passing the address of a local variable to the thread. You said you didn't understand his statement, but that is precisely why your code blows up...you don't understand it.

    Here is a breakdown for you. When a variable is declared inside a function, it is a "local" variable. That means when the function exits, the variable is deallocated also, because the variable was allocated on the stack. What you need is a variable that is allocated either in heap memory, or in global space. To make a variable in global space, declare the variable outside of any function body. To make it on heap space, use malloc().

    The reason you can't use local variables with threads by the way, is that when pthread_create is called, you have no guarantees when the thread routine will be called. Sometimes the thread will be created, the function will run, and complete, before the pthread_create call has even returned (in this case when the thread references the local variable things will run ok). Probably on your "old" distribution this is what was happening. In other cases, pthread_create might return, and the function that called it might exit, all before the thread routine even starts--in this case your local variable is deallocated and not available to the thread when it runs (hence the seg fault).
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM