About pthread_create

This is a discussion on About pthread_create within the C Programming forums, part of the General Programming Boards category; Hello everyone! I just started working on threads, and I'm having trouble understanding the last argument passed to pthread_create(). What ...

  1. #1
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    About pthread_create

    Hello everyone!

    I just started working on threads, and I'm having trouble understanding the last argument passed to pthread_create().

    What is the void * arg?
    Does that mean I can pass a pointer to any kind of data type?
    I'm loking for an example, but the only I find are with int.

    Thanks
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here is a decent reference.

    'arg' is passed as an argument to the 'start' function. It is a void pointer so that it can take various forms of data. If your start function wants a structure called 'foo', then you pass it said structure. If it requires an integer, you pass it that.

    Void pointers can be passed any data type.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Thank Quzah...

    Now, I'm guessing you can't pass more than that one argument to the start function. The only way to pass more than one would be to create global variables. Am I right?
    Is there any other way?
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Lau
    Thank Quzah...

    Now, I'm guessing you can't pass more than that one argument to the start function. The only way to pass more than one would be to create global variables. Am I right?
    Is there any other way?
    Create a structure to hold all of the variables you want to pass it, then make the start function read them however you need it to.
    Code:
    struct args
    {
        int anInt;
        char aString[BUFSIZ];
        float aFloat;
    };
    Like so.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    oh! That makes sense!

    Thanks again
    No more questions
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    You must pass a pointer to
    - a global variable
    - a malloc'ed variable
    - a local variable which will remain in scope for the life of the created thread. This could be either because the variable is in a function which doesn't return until the thread completes, or the variable is static.

    If the variable goes out of scope before the created thread completes, then you're out of luck.
    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.

  7. #7
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    I was going to use global variables!

    Actually, I do have another question: would you recommend creating new threads from a 'start' function?
    The assignment I have to do requires me to pass a buffer from a thread to another to antoher, ect. I was thinking (although not really) top call pthread_create() from a start function and that would call pthread_create(), and so on. Like I said, I haven't though about it too much, but is it something to avoid maybe?
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  8. #8
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    I am wondering something again:
    is it ok to call pthread_join() from a start function?
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

Popular pages Recent additions subscribe to a feed

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