C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 12th, 2008, 05:18 PM
Lesley_B's Avatar
Lesley_B Lesley_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 55 Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 21 h 10 m 18 sec
Reputation Power: 16
Understanding and using void * and void ** in pthread_exit and pthread_join

Hi

Debian etch, gcc version 4.1.2

I'm having a bit of a problem returning data from a thread.
This problem probably extends into a 'what is (void **) ?' question as well.

I have one thread retrieving some data off a queue.
Another thread (listening thread) receives data and then kicks off another thread (adding thread) to to add to the queue.

This bit of code is the control for the retrieving thread
Code:
64     char cbuff[RD_BUFF+1];
...

122     while (1) {
123         sleep(1);
124         pthread_mutex_lock(&qu_mutex);
125         if (start != NULL ) {
126             snprintf(str,MAXLINE-1, "MAIN : Calling  collector thread \n");
127             do_log(fd, str);
128             //memset(&event,'\0',sizeof(event));
129             memset(cbuff,'\0',RD_BUFF+1);
130             pthread_create(&collect_tid, attr, eventCollector, (void *)(&cbuff));
131             pthread_join(collect_tid,(void *)cbuff);
132             snprintf(str,MAXLINE-1, "MAIN : Received from collector thread %s\n",cbuff);
133             do_log(fd, str);
134             qu_log();
135         }
136         pthread_mutex_unlock(&qu_mutex);
137     }


Given I intend to wait for the completion of thread execution it hardly seems worth having a thread here. However, I shall for the moment persist.

The retrieving thread removes an element at the start of the queue (FIFO arrangement) and readjusts the pointer to the start of the queue.

Code:
288 void *eventCollector(void *data){
289     // gets an event from the event queue
290     //struct ii_thdata *ptr ;
291     char *ptr ;
292     char str[MAXLINE];
293
294     //ptr = (struct ii_thdata *) data;
295     //memset(ptr,'\0',sizeof(ii_thdata));
296     ptr = ( char *) data;
297     memset(ptr,'\0',RD_BUFF+1);
298     if (start != NULL) {
299         strncpy(ptr,start->buff,RD_BUFF);
300         snprintf(str,MAXLINE-1, "EVCOLL : Queue start address before deletion %x\n",start);
301         do_log(fd, str);
302         qu_del(1);
303         snprintf(str,MAXLINE-1, "EVCOLL : Queue start address after deletion %x\n",start);
304         do_log(fd, str);
305     }
306     pthread_exit((void*)ptr);
307 }
308


I am not getting back the data correctly in L131. L132 prints out

Code:
Thu Jun 12 22:04:59 2008 MAIN : Received from collector thread �F� is test buffer 8


fairly consistently. I have a fixed data set to test this with and the only variation is in the digit.

Here's some log content showing addition to the queue, demonstrating the queue data isn't corrupt on addition. (Code for queue addition isn't shown here.)
Code:
 40 Thu Jun 12 22:04:41 2008 EVADD : In eventAdd
 41 Thu Jun 12 22:04:41 2008 EVADD : received data  "This is test buffer 1"
 42 Thu Jun 12 22:04:41 2008 EVADD : Added "This is test buffer 1" to queue
 43 Thu Jun 12 22:04:41 2008 QULOG : Queue start element address 0x804d7f8
 44 Thu Jun 12 22:04:41 2008 QULOG : Queue element This is test buffer 0 (0x804d7f8)
 45 Thu Jun 12 22:04:41 2008 QULOG : Queue element This is test buffer 1 (0x804dc98)
 46 Thu Jun 12 22:04:41 2008 EVADD : Leaving eventAdd


So ... it appears I get the data into the queue allright.

The prototypes of pthread_create and pthread_join are

Code:
int pthread_create(pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg);
int pthread_join(pthread_t th, void **thread_return);


I simply don't understand how to change cbuff to a void * to pass it in, nor how to get the data out again using void ** .

I understand that void * is a pointer to something, the shape of which is unknown at compile time.
I understand that one cannot deference a void *.

I interpret char ** as a pointer to a pointer to a char
I have used char ** for an array of strings and int ** for a matrix of integers, similarly for float ** and double **.

I can therefore interpret void ** as a pointer to a pointer to an object of unknown shape or size.

But I am stumped w.r.t. converting from a void ** to anything 'useful' e.g. char, char * .

I'm not understanding void ** and void * well enough to be able to take a char[] or char * and convert between the two.

Can anyone help here?

Regards

Lesley

Reply With Quote
  #2  
Old June 12th, 2008, 09:43 PM
Lesley_B's Avatar
Lesley_B Lesley_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 55 Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 21 h 10 m 18 sec
Reputation Power: 16
Hi

I've realised that malloc returns a void pointer which can be cast as a pointer to any type.

To pass a character string cbuff into a thread, the void *arg in the pthread_create prototype can be replaced with
Code:
 (void *) cbuff

which is simple type casting.

I have also corrected the code so that the pthread_exit call is returning a void *, namely the one it came in with, i.e. L306 has been replaced with
Code:
pthread_exit(data);


For the void ** argument of pthread join, (L 131) I defined a void pointer,
Code:
void *status;
and then used
Code:
pthread_join(collect_tid,&status);


which makes much more sense.
I continued to print out cbuff as before and this time it came out correct.



L.

Reply With Quote
  #3  
Old June 12th, 2008, 11:18 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 15 m 20 sec
Reputation Power: 1774
In main, you'd have something like this
Code:
char cbuff[RD_BUFF+1];
void *result;
char *actualResult;
///
pthread_create(&collect_tid, attr, eventCollector, (void *)cbuff);
pthread_join(collect_tid,&result);
actualResult = result;
// now do stuff with the char* actualResult, like print it with %s

You might not even need the cast in the create thread call either. The compiler should treat it as an assignment to void* of another pointer type.

The thread code itself seems fine as it is, although some of the casts do seem redundant. In C, assigning a pointer type to void* and back again is perfectly legal.
__________________
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

Reply With Quote
  #4  
Old June 13th, 2008, 12:00 PM
Lesley_B's Avatar
Lesley_B Lesley_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 55 Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level)Lesley_B User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 21 h 10 m 18 sec
Reputation Power: 16
Hi salem

The code compiles and runs okay without the cast in the pthread_create call and also without the char *ptr in the thread at all i.e. just operating on the void *data argument. Okay so long as I don't need ever to dereference the pointer.

Not sure how compiler dependent the treating of a void * as a char * is but this code is not likely to be used anywhere anyway.

I changed the pthread-exit command to
Code:
pthread_exit(data);

Earlier on, I was making the mistake of treating that return value as a void ** not a void * - which wasn't helping much.

I'm happy. I have a handle on the void And thank you for your help.

Regards

Lesley

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Understanding and using void * and void ** in pthread_exit and pthread_join

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap