Simple Detached pthread does not cancel! (cout blocks and interleaves even if mutexed)



  • I have a hard problem here, which I can not solve and do not find the right answer on the net:

    I have created a detached thread with a clean up routing, the problem is that on my Imac and Ubuntu 9.1 (Dual Core). I am not able to correctly cancel the detached thread in the fallowing code:

    #include <iostream>
    #include <pthread.h>
    #include <sched.h>
    #include <signal.h>
    
    #include <time.h>
    
    pthread_mutex_t mutex_t;
    
    using namespace std;
    
    static void cleanup(void *arg){
        pthread_mutex_lock(&mutex_t);
        cout << " doing clean up"<<endl;
        pthread_mutex_unlock(&mutex_t);
    }
    
    static void *thread(void *aArgument)
    {
    
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
    
        pthread_cleanup_push(&cleanup,NULL);
        int n=0;
        while(1){
            pthread_testcancel();
            sched_yield();
            n++;
    
            pthread_mutex_lock(&mutex_t);
            cout << " Thread 2: "<< n<<endl;  // REMOVE <<ENDL; and it works!! bUT WHY???
            pthread_mutex_unlock(&mutex_t);
    
        }
        pthread_cleanup_pop(0);
    
        return NULL;
    }
    
    int main()
    {
    
        pthread_t thread_id;
    
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    
        int error;
    
        if (pthread_mutex_init(&mutex_t,NULL) != 0) return 1;
    
        if (pthread_create(&thread_id, &attr, &(thread) , NULL) != 0) return 1;
    
        pthread_mutex_lock(&mutex_t);
        cout << "waiting 1s for thread...\n" <<endl;
        pthread_mutex_unlock(&mutex_t);
    
        int n =0;
    
        while(n<1E3){
            pthread_testcancel();
            sched_yield();
            n++;
    
            pthread_mutex_lock(&mutex_t);
            cout << " Thread 1: "<< n<<endl;
            pthread_mutex_unlock(&mutex_t);
        }
    
        pthread_mutex_lock(&mutex_t);
        cout << "canceling thread...\n" <<endl;
        pthread_mutex_unlock(&mutex_t);
    
        if (pthread_cancel(thread_id) == 0)
        {
            //This doesn't wait for the thread to exit
            pthread_mutex_lock(&mutex_t);
            cout << "detaching thread...\n"<<endl;
            pthread_mutex_unlock(&mutex_t);
    
            pthread_detach(thread_id);
    
            while (pthread_kill(thread_id,0)==0)
            {
                    sched_yield();
            }
    
            pthread_mutex_lock(&mutex_t);
            cout << "thread is canceled";
            pthread_mutex_unlock(&mutex_t);
    
        }
    
        pthread_mutex_lock(&mutex_t);
        cout << "exit"<<endl;
        pthread_mutex_unlock(&mutex_t);
    
        return 0;
    }
    

    When I replace the Cout with printf() i workes to the end "exit" , but with the cout (even locked) the executable hangs after outputting "detaching thread...
    When I remove the <<endl; in the thread (see code) then it works?? But why??

    It would be very cool to know from a Pro, what the problem here is?. Why does this not work even when cout is locked by a mutex!?

    Thanks a lot for your support!!


Anmelden zum Antworten