Kill DETACHED Thread

1,072 views
Skip to first unread message

Nandhan Thiravia

unread,
Mar 13, 2012, 11:48:01 AM3/13/12
to android-ndk
Hi

I am trying to make an application for Android regarding Audio
Streaming. I have created a thread which is a detached one and which
keeps on downloading data packets from the network and the main thread
plays the data packets simultaneously.

The problem lies in the fact that I need to "kill" the detached thread
once there happens any error while reading the data packets from
network. In that case I have tried to cancel the detached thread
because I found no other way to do it using pthread_cancel() function
but in order to compile it for Android platform I need to compile in
Android NDK and it says "PTHREAD_CANCEL_ENABLE" parameter is not
known !! Please help in this issue

Thanks
Nandhan

David Turner

unread,
Mar 13, 2012, 12:14:19 PM3/13/12
to andro...@googlegroups.com
Using pthread cancellation is a sure way to write buggy code that leaks resources and randomly misbehaves.
That's why this "feature" is not supported in Android.

The only proper way for a thread to die is to exit on its own. This guarantees that this happens when the thread's state is well-known, and not inside some C library function which has acquired some locks or other resources (just as an example).

Your thread should be listening to both network packets and a pipe file descriptor, which another thread can send a byte to to instruct early termination.

Nandhan

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Nils Hermansson

unread,
Mar 14, 2012, 3:37:08 AM3/14/12
to andro...@googlegroups.com
This I do not understand, if you use pthread_testcancel and
PTHREAD_CANCEL_DEFERRED it should be no risk with buggy code. Bah now
I need to rewrite how threads cancels. Is there any documentation what
Android has decided to ripped/disabled in c libs, since I prefer to
write code for linux x86 first and then merge it into my android
project(Haven't gotten there yet).

Regards.

David Turner

unread,
Mar 14, 2012, 8:25:08 AM3/14/12
to andro...@googlegroups.com
On Wed, Mar 14, 2012 at 8:37 AM, Nils Hermansson <3tn...@gmail.com> wrote:
This I do not understand, if you use pthread_testcancel and
PTHREAD_CANCEL_DEFERRED it should be no risk with buggy code.

Consider the following code:

    {
       char* tmp = malloc(4096);
       pthread_mutex_lock(&someLock);
       foo(tmp);
       pthread_mutex_unlock(&someLock);
       free(tmp);
    }

If pthread cancelation occurs inside of foo(), because the function reaches any cancellation point, then you have leaked the 'tmp' buffer, and kept your mutex locked.
Unless you want to use __pthread_cleanup_push / __pthread_cleanup_pop extensively, but this doesn't scale at all.
 
Bah now
I need to rewrite how threads cancels. Is there any documentation what
Android has decided to ripped/disabled in c libs, since I prefer to
write code for linux x86 first and then merge it into my android
project(Haven't gotten there yet).

pthread cancelation and sysV IPCs are not implemented on purpose. The rest is missing for historical reasons (i.e. mostly because the platform didn't need it).
 
Reply all
Reply to author
Forward
0 new messages