Thread: pthread question

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    pthread question

    Hi All,

    Not sure where to put pthread_attr_init and setdetachstate. Should they be called once for many threads as below or each instance of a new thread:
    Code:
    res = pthread_attr_init(&thread_attr);
                            if (res != 0)
                            {
                                    syslog(LOG_ERR,":Error - Server thread error: %s",strerror(res));
                                    return 5;
                            }
                            res = pthread_attr_setdetachstate(&thread_attr,1);//PTHREAD_CREATE_DETACHED);
                            if (res != 0)
                            {
                                    syslog(LOG_ERR,":Error - Server thread error: %s",strerror(res));
                                    return 6;
                            }
    
                            for(;;)
                            {
                                    cli_size = sizeof(cli);
                                    client = accept(s, (struct sockaddr *)&cli, &cli_size);
                                    if (client == -1)
                                    {
                                            syslog(LOG_ERR,":Error - Server thread error: %s",strerror(client));
                                            return 7;
                                    }
    
                                    if(cob_silent)
                                            printf("Got client from %s\n", inet_ntoa(cli.sin_addr));
    
                                    res = pthread_create(&tid,&thread_attr,HandleRequest,&client);
                                    if (res != 0)
                                    {
                                            syslog(LOG_ERR,":Error - Server thread error: %s",strerror(res));
                                            return 8;
                                    }
                                    pthread_detach(tid);
                            }
    Thanks for your help,

    rotis23

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > res = pthread_create(&tid,&thread_attr,HandleRequest,&cl ient);
    I dunno about the attribute thing, but this line is wrong.
    You're passing a pointer to a local (or global) variable, which could well change before the created thread has a chance to get hold of it.

    Ideally, you should malloc some 'thread instance' memory, store your client in that memory, and then pass that memory to the thread proper (which will delete it when it finishes).

    int *p = malloc ( sizeof *p );
    *p = client;
    res = pthread_create(&tid,&thread_attr,HandleRequest,p);
    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.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    OK, thanks - I'll take a look.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Salem,

    I see.

    The client value will change when the next connection is made i.e. with the new connection descriptor. Although I do make a copy immediately within HandleRequest it still could conceivably change during that time.

    It's currently a local variable. I've always been unclear about the scope of local variables and whether it's safe to pass their address (using &) to another function. Or even returning the address of a local variable from function. These instances compile but are surely unsafe?

    Can anyone confirm?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Passing &foo to a called function is safe, within the same thread.

    These are unsafe - return address of a local, or pass address of local to another thread
    return &foo;
    pthread_create( ...., &foo );
    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.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Cheers.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    OK, is it safe to:
    Code:
    struct test mytest;//struct consists of just 2 ints.
    
    mytest.x = 1;
    mytest.y = 34;
    
    res = pthread_create(&tid,&thread_attr,HandleRequest,&mytest);
    Will this be shared?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Didn't I answer this already?
    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.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Well yes, but I've also read (on this list I think) that structs are explicitly copied when passed as an argument to a function.

    I'm just unsure whether this applies in the special pthread_create case - obviously not.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > that structs are explicitly copied when passed as an argument to a function.
    Yes they are, but you're not passing a struct, your passing a POINTER to a struct.
    So the same rules about the scope still apply.
    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.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Fair enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. The Pthread Hell
    By matott in forum Linux Programming
    Replies: 1
    Last Post: 04-10-2005, 05:59 AM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM