1

I am writing code for my first tool which supports multi threading but also I want to make my tool for both Linux & Android , and pthread_cancel can't be used in android so I am trying to make separate header file universal-threading.h in which I will declare pthread_cancel function for android.

So when it will compile on Android then universals-threading.h will be included otherwise not.

I made also a function of pthread_setcancelstate for android Here is snippet

static int pthread_setcancelstate(int state, int *oldstate) {
    sigset_t   new, old;
    int ret;
    sigemptyset (&new);
    sigaddset (&new, SIG_CANCEL_SIGNAL);

    ret = pthread_sigmask(state == PTHREAD_CANCEL_ENABLE ? SIG_BLOCK : SIG_UNBLOCK, &new , &old);
    if(oldstate != NULL)
    {
        *oldstate = sigismember(old,SIG_CANCEL_SIGNAL) == 0 ? PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE;
    }
    return ret;
}

But now I am confused with pthread_setcanceltype function so it will be very helpful for me if you will give a working code block or snippet of pthread_setcanceltype android version .

In short I want to create a fake pthread_setcanceltype function for android

I am not professional its my first tool and I am also new to threading and pthreads so I hope you will consider this while answering me .

imbr
  • 6,226
  • 4
  • 53
  • 65
Golu
  • 350
  • 2
  • 14
  • 3
    Design so that pthread_cancel() is not required. There, job done. – Martin James Mar 20 '18 at 08:46
  • Sorry I didn't get you, can you please explain in brief :) – Golu Mar 20 '18 at 09:10
  • 1
    @Golu He is saying you should change your code so that you do not need to call `pthread_setcanceltype`. (Note that as Android does not natively support `pthread_setcanceltype` , it's pretty impossible for anyone here to come up with a version that will work on Android - you really need native support for it on the platform.) – nos Mar 20 '18 at 12:57
  • 1
    I can see many projects in github which were ported to android by patching (adding fake setcancelstate and pthread_cancel functions ) what about those ? And I am agree with you I should change my code for better future of my tool but just for knowledge I want to make a fake setcanceltype function and I am unable to find example on github because I found only few projects with fake pthread_setcancelstate but not setcanceltype. – Golu Mar 20 '18 at 17:30
  • @Golu long time after... Hope you found your answer. The code for `pthread_setcancelstate` is awesome thank you very much! – imbr Feb 16 '21 at 12:55

0 Answers0