![]() |
| | #1 |
| Registered User Join Date: Aug 2002
Posts: 351
| pthread question 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);
}
rotis23 |
| rotis23 is offline | |
| | #2 |
| and the hat of Jobseeking 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); |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Aug 2002
Posts: 351
| OK, thanks - I'll take a look. |
| rotis23 is offline | |
| | #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 | |
| | #5 |
| and the hat of Jobseeking 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 ); |
| Salem is offline | |
| | #6 |
| Registered User Join Date: Aug 2002
Posts: 351
| Cheers. |
| rotis23 is offline | |
| | #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); |
| rotis23 is offline | |
| | #8 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,674
| Didn't I answer this already? |
| Salem is offline | |
| | #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 | |
| | #10 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #11 |
| Registered User Join Date: Aug 2002
Posts: 351
| Fair enough. |
| rotis23 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |