|
Home > Archive > Unix Programming > October 2006 > linux / solaris
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| shaanxxx 2006-10-16, 1:27 pm |
| I have following programme :
#include<stdio.h>
#include<pthread.h>
#include <unistd.h>
pthread_t thr;
class test{
public :
test(){printf("cons\n");}
~test(){printf("destructor \n");}
};
extern "C" void *thrFn(void *c)
{
test mytest;
sleep(1000);
return 0;
}
int main()
{
void *a ;
pthread_create(&thr,0,thrFn,0);
pthread_cancel(thr);
pthread_join(thr,&a);
}
when i run on solaris , i get .
sun: ./a.out
cons
destructor
when i run on linux , i get
linux: ./a.out
cons
Which one is correct behaviour?
Why am i getting different behaviour?
| |
| Maxim Yegorushkin 2006-10-16, 1:27 pm |
|
shaanxxx wrote:
> I have following programme :
>
> #include<stdio.h>
> #include<pthread.h>
> #include <unistd.h>
> pthread_t thr;
> class test{
> public :
> test(){printf("cons\n");}
> ~test(){printf("destructor \n");}
>
>
> };
> extern "C" void *thrFn(void *c)
> {
> test mytest;
> sleep(1000);
> return 0;
> }
> int main()
> {
> void *a ;
> pthread_create(&thr,0,thrFn,0);
> pthread_cancel(thr);
> pthread_join(thr,&a);
> }
>
>
> when i run on solaris , i get .
>
> sun: ./a.out
> cons
> destructor
>
>
> when i run on linux , i get
> linux: ./a.out
> cons
>
> Which one is correct behaviour?
Both. Another correct behaviour is that there may be no output at all.
> Why am i getting different behaviour?
Because there is a race between your threads. It is not known how much
progress your new thread has done by the time you call pthread_cancel()
in the main thread.
| |
| shaanxxx 2006-10-16, 1:27 pm |
|
shaanxxx wrote:
> I have following programme :
>
> #include<stdio.h>
> #include<pthread.h>
> #include <unistd.h>
> pthread_t thr;
> class test{
> public :
> test(){printf("cons\n");}
> ~test(){printf("destructor \n");}
>
>
> };
> extern "C" void *thrFn(void *c)
> {
> test mytest;
> sleep(1000);
> return 0;
> }
> int main()
> {
> void *a ;
> pthread_create(&thr,0,thrFn,0);
> pthread_cancel(thr);
> pthread_join(thr,&a);
> }
>
>
> when i run on solaris , i get .
>
> sun: ./a.out
> cons
> destructor
>
>
> when i run on linux , i get
> linux: ./a.out
> cons
>
>
>
> Which one is correct behaviour?
> Why am i getting different behaviour?
modification to above programme
#include<stdio.h>
#include<pthread.h>
#include <unistd.h>
pthread_t thr;
class test{
public :
test(){printf("cons\n");}
~test(){printf("destructor \n");}
};
extern "C" void routine(void *)
{
printf("handler \n");
}
extern "C" void *thrFn(void *c)
{
test mytest;
pthread_cleanup_push(routine, 0);
printf("sizeof ptr %d\n", (int)sizeof(void *));
sleep(1000);
pthread_cleanup_pop(1);
return 0;
}
int main()
{
void *a ;
pthread_create(&thr,0,thrFn,0);
sleep(1);
pthread_cancel(thr);
pthread_join(thr,&a);
}
out put on solaris :
cons
sizeof ptr 4
handler
destructor
output on linux :
cons
sizeof ptr 4
destructor
handler
| |
| shaanxxx 2006-10-16, 1:27 pm |
|
Maxim Yegorushkin wrote:
> shaanxxx wrote:
>
> Both. Another correct behaviour is that there may be no output at all.
>
>
> Because there is a race between your threads. It is not known how much
> progress your new thread has done by the time you call pthread_cancel()
> in the main thread.
i did some change : stack clean up is expected behaviour ?
| |
| Maxim Yegorushkin 2006-10-17, 7:39 am |
|
shaanxxx wrote:
[]
>
> i did some change : stack clean up is expected behaviour ?
Could you tell us the big picture of what you are trying to do?
| |
| David Schwartz 2006-10-18, 1:36 am |
|
shaanxxx wrote:
> out put on solaris :
> cons
> sizeof ptr 4
> handler
> destructor
>
> output on linux :
> cons
> sizeof ptr 4
> destructor
> handler
You ask which behavior is correct or incorrect, but there is no
standard to refer to. So both are equally correct. AFAIK, there is no
standard for how pthread cancellation handlers interact with C++
destructors.
DS
| |
| shaanxxx 2006-10-18, 7:28 am |
|
David Schwartz wrote:
> shaanxxx wrote:
>
>
> You ask which behavior is correct or incorrect, but there is no
> standard to refer to. So both are equally correct. AFAIK, there is no
> standard for how pthread cancellation handlers interact with C++
> destructors.
Thanks.
I was really confused. When i read man pages on linux , it doesnt say
anything about stack wind up.
But , solaris man page says that there will be call to destructor. When
we see behaviour in linux, it calls destructor.
Thanks again .
| |
| shaanxxx 2006-10-18, 7:28 am |
|
Maxim Yegorushkin wrote:
> shaanxxx wrote:
>
> []
>
>
> Could you tell us the big picture of what you are trying to do?
Thanks for reply.
I was trying to figure out that what is the right behaviour.
Thanks again.
| |
| joe@invalid.address 2006-10-18, 1:29 pm |
| "shaanxxx" <shaanxxx@yahoo.com> writes:
> Maxim Yegorushkin wrote:
>
> Thanks for reply.
>
> I was trying to figure out that what is the right behaviour.
David explained that there is no "right" behavior. The difference you
see in man pages and output between Solaris and Linux is due to
this. Without a standard to specify what should happen, it's up to the
implementation to decide what it's going to do. Solaris decided to do
it one way, and linux decided to do it another way.
You can't expect portable behavior. You have to read the documentation
for each system, and code accordingly.
Joe
|
|
|
|
|