0

I developed a program which create a thread at the beginning. And then I want to kill the thread with pthread_cancel() when the thread is running.

BUt if I do this I get a crash. this crash is not always reproduced. some time the program avoid the crash.

I see some where that pthread_cancel() could cause a crash if the cancelled thread is running a system call. is it truth ?

dmesg shows the following error

myprogramd[1965]: segfault at c ip b76cdef3 sp bfcd5780 error 4 in libpthread-0.9.30.1.so[b76c6000+a000]
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • It's big source code. but I have a doubt that kind of crash is a familiarous crash and is caused `pthread_cancel()`. I think that `pthread_cancel() could cause a crash if the cancelled thread is running a system call – MOHAMED Nov 08 '12 at 15:46
  • 2
    Run the program in a debugger, so that you can capture *where* it crashes. This is better than just coming up with random theories. – unwind Nov 08 '12 at 15:51
  • dmesg show an error indicating that the libpthread is the cause of the crash. question updated – MOHAMED Nov 08 '12 at 15:58

3 Answers3

0

I suspect it to be a synchronization problem. What i mean is in a different thread which is dependent on the thread you are terminating, you might be accessing something which might be invalid and causing the crash. Unless you have the whole code to execute we cannot point out the crash.

At least try running your code on gdb and get the backtrace. Look here

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
CCoder
  • 2,305
  • 19
  • 41
0

Do not use pthread_cancel(). Refactor the code so that it receives a message to close down and not effect the shared memory and make things unstable.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

the crash is related to the manager thread. the manager thread is not properly killed in the first launch so in the second launch when creeating new pthread the old thread manager will cause a crash.

Refer to the following question for more details

How to kill the management thread with C?

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268