C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-31-2004, 04:17 AM   #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
rotis23 is offline   Reply With Quote
Old 03-31-2004, 06:44 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,674
> 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.

Salem is offline   Reply With Quote
Old 03-31-2004, 07:23 AM   #3
Registered User
 
Join Date: Aug 2002
Posts: 351
OK, thanks - I'll take a look.
rotis23 is offline   Reply With Quote
Old 04-01-2004, 04:53 AM   #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?
rotis23 is offline   Reply With Quote
Old 04-01-2004, 05:21 AM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,674
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.

Salem is offline   Reply With Quote
Old 04-01-2004, 05:23 AM   #6
Registered User
 
Join Date: Aug 2002
Posts: 351
Cheers.
rotis23 is offline   Reply With Quote
Old 04-06-2004, 09:10 AM   #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?
rotis23 is offline   Reply With Quote
Old 04-06-2004, 09:30 AM   #8
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,674
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.

Salem is offline   Reply With Quote
Old 04-06-2004, 11:50 AM   #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.
rotis23 is offline   Reply With Quote
Old 04-06-2004, 01:09 PM   #10
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,674
> 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.

Salem is offline   Reply With Quote
Old 04-07-2004, 02:57 AM   #11
Registered User
 
Join Date: Aug 2002
Posts: 351
Fair enough.
rotis23 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:36 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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