pthread_exit and pthread_join usage


 
Thread Tools Search this Thread
Top Forums Programming pthread_exit and pthread_join usage
# 1  
Old 06-10-2010
pthread_exit and pthread_join usage

Hi...
this simple code has warning messages but it work..
Code:
void *th(void *g){
        int y = 10;
        pthread_exit((void*)y);
}
int main(int argn, char ** argp){
        void * ret;
        pthread_t thread;
        pthread_create(&thread, NULL, th, NULL);
        pthread_join(thread, &ret);
        int nm = (int*)ret;
        printf("%d\n", nm);
        return 0;
}

Can I modify for remove any warning message? (sorry for my english if there are errors)
prompt
# 2  
Old 06-10-2010
Quote:
Can I modify for remove any warning message? (sorry for my english if there are errors)
What warning messages are you getting? Also, please post the complete source file that gives you the warnings you want to get rid of, as well as the command you use to compile.
# 3  
Old 06-10-2010
Thanks for you answer...
This is the command
Code:
gcc p2.c -o p2 -l pthread

p2.c: In function ‘th':
p2.c:5: warning: cast to pointer from integer of different size
p2.c: In function ‘main':
p2.c:12: warning: initialization makes integer from pointer without a cast

The code at the first post is complete! Is only a test!
The version of gcc is 4.2.1 and I'm on Mac OSX!
prompt
# 4  
Old 06-10-2010
Quote:
Originally Posted by prompt
The code at the first post is complete! Is only a test!
The version of gcc is 4.2.1 and I'm on Mac OSX!
Well, I thought maybe your warnings were all from not using #include if it's not there - but they'd probably be errors. Also, posting the complete source (including #include directives) means that the warning messages you print will mean something - if your code were complete, you'd have errors at the definition of main() and your return statement that just don't match up - luckily it's quite simple code, and working out where the errors "really" are isn't much of a problem.

Trust me, cutting and pasting the complete source when you post error messages helps.

Either way, missing includes is obviously not your problem.

Your first problem is several problems. In "pthread_exit((void *)y)", you're telling pthread_exit there's a return value at address 10 - I'm surprised it doesn't segfault*. You want the result to be stored at the address of y, so it should be "pthread_exit(&y)".

Your second problem is also the culmination of two problems - instead of declaring "ret" to be a "void *", you may as well be more type-safe and declare it an "int *", and then "int nm = (int*)ret;" should become "int nm = *ret;".

As a third point - I hope you can see that the number of things wrong in such a short piece of code should ring some alarm bells. You don't seem to have fully grasped the concepts of pointers or types. You should definitely find a tutorial on pointers and have a good study of it - go through all the exercises, make sure you understand all of it. They're very important, and can lead to subtle and hard-to-detect bugs (as well as your programs haemorrhaging segfaults) if misused. This code may work, but that's only because you misuse your pointers one way, then manage to misuse them in exactly the opposite way.


* Mind you, the different sizes problem is weird - are you programming for an embedded device or something?

P.S. Oh, but then y is on the stack in th(). You probably don't want that - make it static or put it outside the function definition (or malloc() it), but make sure it's not on the stack.

Last edited by JohnGraham; 06-10-2010 at 10:26 AM..
# 5  
Old 06-10-2010
I'm programming for a school's project!

I read this iSeries Information Center

The code complete is
Code:
#include <pthread.h>
#include <stdio.h>
void *th(void *g){
        int y = 10;
        pthread_exit((void*)y);
}
int main(int argn, char ** argp){
        void * ret;
        pthread_t thread;
        pthread_create(&thread, NULL, th, NULL);
        pthread_join(thread, &ret);
        int nm = (int*)ret;
        printf("%d\n", nm);
        return 0;
}


I know that my code is very strange, but my code is only a copy of the example! I know (I'm not a programmer, I'm only a student) how to use the pointer but only in this way I can retrieve a value!

(I renew my apologies for my english)
prompt
# 6  
Old 06-10-2010
Quote:
Originally Posted by prompt
(I renew my apologies for my english)
No need - your English is perfectly readable.

What do __VOID and __INT expand to? I can't seem to find their definitions. They put theStatus outside of any function (i.e. it's not on the stack) so that makes me think they're something like:

Code:
#define __VOID(X) ((void*)(&X))

(and similarly for int)

This would also give correct code, and would make the pointers point to the return value (good practice) instead of being tread as the return value (awful practice). Give it a go - the compiler will like it a lot more!
# 7  
Old 06-10-2010
Ok.. now the compiler is quiet...
I changed my code
Code:
#include <pthread.h>
#include <stdio.h>


#define __VOID(x) ((void*)(&x))
#define __INT(x) ((int*)(&x))

int y = 10;

void *th(void *g){
        pthread_exit(__VOID(y));
}
int main(int argn, char ** argp){
        void *ret;
        pthread_t thread;
        pthread_create(&thread, NULL, th, NULL);
        pthread_join(thread, &ret);
        int *nm = __INT(ret);
        printf("%d\n", *nm);
        return 0;
}

But my value is incorrect!
prompt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory usage per user,percent usage,sytem time in ksh

Let's say i have 20 users logged on Server. How can I know how much memory percent used each of them is using with system time in each user? (2 Replies)
Discussion started by: roy1912
2 Replies

2. Programming

pthread_exit query

Hi, I am new to multithreaded programming. When creating a thread to run a specific function, does the function need to have a pthread_exit ? Is pthread_exit analogous to a return in a function? (3 Replies)
Discussion started by: sanjayc
3 Replies

3. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

4. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

5. Programming

Doubt on pthread_exit and pthread_join

Main function creates Thread0 and Thread1 by using pthread_create systemcall. In Thread0() { we are calling pthread_exit(0) ; } and in Thread1() { status= pthread_join(tid,NULL); sprintf(ebuf,"timer6: can't join with thread0, status: %d",status); Assert(status==0,ebuf); } ... (4 Replies)
Discussion started by: mansa
4 Replies

6. UNIX for Advanced & Expert Users

pthread_join function

Hi, I would like to know if the call of pthread_join( thread,&status) for a thread already created in main function will free the memory allocated to thread after the pthread_join retruns or should I wait the termination of main function? Is there any need to cancel or exit the thread if I... (0 Replies)
Discussion started by: Behnaz
0 Replies

7. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

8. Linux

pthread_join hang on glibc2.5

Hello All, The problem i'm experiencing is with the following code: #include <pthread.h> #include <stdio.h> int main(void) { (void) pthread_join(155555, NULL); printf("done"); return 0; } I'm getting on terminal segmentation fault . System used:... (0 Replies)
Discussion started by: crocodil
0 Replies

9. UNIX for Advanced & Expert Users

FATAL:exception occured with pthread_exit()

We had written an application in which we create worker thread. So the main thread will create the worker thread. After some time the child thread(Worker thread) will call pthread_exit(). This function was written in try{} and there occured an Exception and is handled in catch(...)... (0 Replies)
Discussion started by: platso
0 Replies

10. UNIX for Dummies Questions & Answers

How to write my own version of pthread_join()

I would like to write my own version of pthread_join and some of the other pthread function. Does some know any pages that have som examples of doing this?? (1 Reply)
Discussion started by: bigblop
1 Replies
Login or Register to Ask a Question