Thread: pthread_create arguments

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    pthread_create arguments

    Hi,
    I've been doing some research in order to start working in a project using pthread.h library and I can't figure out what does the third argument (used to determine the function to be invoqued in the new thread) in pthread_create function is.
    This is the function synopsis:

    Code:
    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
    How can you interpret the third argument? I would say it's a double pointer to a void function with a cast to void*. Is it correct? Anyway it doesnt make much sense for me . Can you give me some ideas?

    Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Not quite. It's a pointer to a function that takes a void pointer as an argument and returns a void pointer. An example of such a function would be:
    Code:
    void *foo(void *bar)
    {
        // do some stuff
    }
    ...
    pthread_create(thread, attr, foo, arg);

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It wants the address of your thread function... so it can start it.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    Thanks!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Not quite. It's a pointer to a function that takes a void pointer as an argument and returns a void pointer. An example of such a function would be:
    Code:
    void *foo(void *bar)
    {
        // do some stuff
    }
    ...
    pthread_create(thread, attr, foo, arg);
    OR....

    Code:
    void foo(void *bar)
    {
        // do some stuff
    }
    ...
    pthread_create(thread, attr, &foo, arg);

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by CommonTater View Post
    OR....

    Code:
    void foo(void *bar)
    {
        // do some stuff
    }
    ...
    pthread_create(thread, attr, &foo, arg);
    Nope. Reread that declaration carefully:
    Code:
    void *(*start_routine) (void *)
    start_routine is a pointer to a function
    that takes a void pointer
    and returns a void pointer

    And as far as function pointers go, passing foo and &foo are the same thing. There was a discussion about this in a thread about 2 or 3 weeks ago, and cas (I think it was cas) explained it very well. I'll see if I can dig it up.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Nope. Reread that declaration carefully:
    Code:
    void *(*start_routine) (void *)
    start_routine is a pointer to a function
    that takes a void pointer
    and returns a void pointer

    And as far as function pointers go, passing foo and &foo are the same thing. There was a discussion about this in a thread about 2 or 3 weeks ago, and cas (I think it was cas) explained it very well. I'll see if I can dig it up.
    Yeah, I'd appreciate that. I do this in windows using CreateThread() but it's different than this.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    I think this is the thread I was thinking of. I guess it wasn't quite what I remembered, but cas' comment(post #3) still implies that there is no difference between foo and &foo when it comes to function names. Though &&foo becomes a pointer to a pointer to a function.

    There is nothing different in how you would declare a function pointer in Windows, though the pthreads library itself is *NIX only. I haven't done much threading work in Linux, so I don't know much and don't have any good references to give you on that.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    I think this is the thread I was thinking of. I guess it wasn't quite what I remembered, but cas' comment(post #3) still implies that there is no difference between foo and &foo when it comes to function names. Though &&foo becomes a pointer to a pointer to a function.

    There is nothing different in how you would declare a function pointer in Windows, though the pthreads library itself is *NIX only. I haven't done much threading work in Linux, so I don't know much and don't have any good references to give you on that.
    Well, thanks for what you did find.

    The CreateThread() call in Windows explicitly wants the address of the function... which can be any function you choose... That's why I mentioned it. It would appear that --per usual-- Linux is somewhat different...

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    I still think you're confused. The parameter is of type "pointer to function", but what you pass needs to be the address of a function, just like in Windows. All I'm saying is that in C (on all platforms), for functions only, foo and &foo are equivalent. Both result in the address of the function named 'foo'. Try this in one of your Windows threading programs and let me know. Replace &foo with just foo, and see if it still works and calls the right function without any errors or warnings.

    As a test, I compiled this in VC++ 2010 with warnings maxed out and got not a peep from the compiler:
    Code:
    #include <stdio.h>
    
    void foo(int n)
    {
    	printf("foo %d\n", n);
    }
    
    void bar(int n)
    {
    	printf("bar %d\n", n);
    }
    
    void (*get_fn(int n))(int)
    {
    	return n < 3 ? foo : &bar;  // notice foo has no &, but bar does, and no type mismatch
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int i;
    	void (*fn)(int);
    
    	for (i = 0; i < 10; i++) {
    		fn = get_fn(i);
    		fn(i);
    	}
    
    	return 0;
    }

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    I still think you're confused. The parameter is of type "pointer to function", but what you pass needs to be the address of a function, just like in Windows. All I'm saying is that in C (on all platforms), for functions only, foo and &foo are equivalent. Both result in the address of the function named 'foo'. Try this in one of your Windows threading programs and let me know. Replace &foo with just foo, and see if it still works and calls the right function without any errors or warnings.
    It errors off at least in CreateThread... This I know from hard experience.

    I've also used a few pointers to functions in COM interfaces and there it doesn't seem to care...

    However, I've not messed with it as you demonstrated.... It will be fun to play with.

    Interresting... ya learns something new everyday. Thanks for your time on this. It is appreciated.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,980
    >> The CreateThread() call in Windows explicitly wants the address of the function... which can be any function you choose.
    Any function with the correct signature and calling convention:
    DWORD __stdcall ThreadProc(void*)
    Anything else is wrong - which is why you should never cast the 3rd parameter in a CreateThread call (or pthread_create call).

    You can pass either "&ThreadProc" or just "ThreadProc" when calling CreateThread().

    On some compilers (like Sun's cc compiler), the linkage of the thread function needs to be "C" as well - when compiling as C++.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM