Trouble pthread_create

This is a discussion on Trouble pthread_create within the Linux Programming forums, part of the Platform Specific Boards category; I have NetBSD on i386. I am using the GNU pthread library. When I run the following code, the same ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Trouble pthread_create

    I have NetBSD on i386. I am using the GNU pthread library. When I run the following code, the same integer value is printed in both func1 and func2. The value 89 is printed in both functions.
    Code:
    #include <pthread.h>
    
    void func1(void *n)
    {
    	int *a = (int *) n;
    	printf("hello: %i\n", *a);
    }
    
    void func2(void *r)
    {
    	int *b = (int *) r;
    	printf("hello again: %i\n", *b);
    }
    
    void make_thread(pthread_t *thread, void (*func)(void *), int value)
    {
    	printf("value: %i\n", value);
    	pthread_create(thread, NULL, (void *) (func),  &value);
    }
    
    int main()
    {
    	int *retval;
    	pthread_t thread1, thread2;
    
    	make_thread(&thread1, &func1, 34);
    	make_thread(&thread2, &func2, 89);
    
    	pthread_join(thread1, (void **) &retval);
    	pthread_join(thread2, (void **) &retval);
    
    	return 0;
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I rewrote the code so the compiler won't give me a warning.
    Code:
    #include <pthread.h>
    
    void *func1(void *n)
    {
    	int *a = (int *) n;
    	printf("hello: %i\n", *a);
    
    	return NULL;
    }
    
    void *func2(void *r)
    {
    	int *b = (int *) r;
    	printf("hello again: %i\n", *b);
    
    	return NULL;
    }
    
    void make_thread(pthread_t *thread, void* (*func)(void *), int value)
    {
    	pthread_create(thread, NULL, func, (void *) &value);
    }
    
    int main()
    {
    	int *retval;
    	pthread_t thread1, thread2;
    
    	make_thread(&thread1, &func1, 34);
    	make_thread(&thread2, &func2, 89);
    
    	pthread_join(thread1, (void **) &retval);
    	pthread_join(thread2, (void **) &retval);
    
    	return 0;
    }
    When I run on NetBSD I get the following results:
    hello: 89
    hello again: 89

    But when I run of IRIX 6.5, I get the following:
    hello: 2147430336
    hello again: 2147430336

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > pthread_create(thread, NULL, func, (void *) &value);
    You're passing a pointer to a local variable - which goes out of scope the moment the function exits, and which is therefore no longer valid when you dereference it in your thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Thanks

    Then how else would I do it?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > Then how else would I do it?
    The pointer you pass to pthread_create() has to last up to the pthread_join() call.
    In your case, this means
    - a local var in main()
    - a global variable
    - a pointer returned from malloc()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Ok, here's what I did, and it worked:
    Code:
    include <pthread.h>
    
    void *func1(void *n)
    {
            int *a = (int *) n;
            printf("hello: %i\n", *a);
    
            return NULL;
    }
    
    void *func2(void *r)
    {
            int *b = (int *) r;
            printf("hello again: %i\n", *b);
    
            return NULL;
    }
    
    void make_thread(pthread_t *thread, void* (*func)(void *), int *value)
    {
            pthread_create(thread, NULL, func, value);
    }
    
    int main()
    {
            int *retval, i = 34, j = 89;
            pthread_t thread1, thread2;
    
            make_thread(&thread1, &func1, &i);
            make_thread(&thread2, &func2, &j);
    
            pthread_join(thread1, (void **) &retval);
            pthread_join(thread2, (void **) &retval);
    
            return 0;
    }
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 11:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2003, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21