I'm linking a C++ source with a C source and a C++ source. I create a thread with pthread, a cancellation point and then I call pthread_exit either through the C or the C++ source file.
If the pthread_exit call comes from the C source, the cancellation handler does not fire! What may be the reason for this?
b.cc:
#include <cstdio>
#include <cstdlib>
#include <stdbool.h>
#include <pthread.h>
extern "C" void V();
extern "C" void Vpp();
extern "C" void Vs();
#define PTHREAD_EXIT Vs
void cleanup(void*v)
{
fprintf(stderr, "Aadsfasdf\n");
exit(0);
}
void* f(void*p)
{
pthread_cleanup_push(cleanup, NULL);
PTHREAD_EXIT();
pthread_cleanup_pop(true);
return NULL;
}
int main()
{
pthread_t p;
if (pthread_create(&p, NULL, f, NULL))
abort();
for(;;);
}
vpp.cc:
#include <pthread.h>
extern "C" void Vpp();
void Vpp() {
pthread_exit(0);
}
v.c:
#include <pthread.h>
void V() {
pthread_exit(0);
}
vs.s:
.text
Vs: .global Vs
call pthread_exit
spin: jmp spin
compilation with
g++ -c vpp.cc -g -o vpp.o -Wall
gcc -c v.c -g -o v.o -Wall
as vs.s -o vs.o
g++ b.cc vpp.o v.o vs.o -o b -lpthread -g -Wall
If PTHREAD_EXIT is Vpp the program displays a message and terminates, if it is V or Vs it doesn't.
the disassembly for V and Vpp is identical, and changing the definition of PTHREAD_EXIT between V and Vpp merely changes between call V
and call Vpp
in the disassembly.
EDIT: Not reproducible on another computer, so I guess I hit an error in the library or something.