LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2010, 10:14 PM   #1
sshh2010_2020
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Rep: Reputation: 0
pthread_cancel doesn't work


Hi, i am writing a thread programming on the linux(federol core 8), but i really don't what is wrong with it.
The code is below:
Code:
#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;

pthread_t pid[3];

void* thread_run_1(void* arg)
{
   if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
       return NULL;

    cout<<"Now in the thread 1"<<endl;

    int state, oldstate;

    state = PTHREAD_CANCEL_DEFERRED;
    pthread_setcancelstate(state, &oldstate);

    cout<<"oldstate is "<<(state == oldstate? "Deferred":"Async")<<endl;


	
    for (int i = 1; i<= 1e9; ++i);  //wait for thread2 to cancel this thread	
    //sleep(2);			//wait for thread2 to cancel this thread
   
    cout<<"before testcancel"<<endl;
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
    pthread_testcancel();
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);

    cout<<"after testcancel"<<endl;
    cout<<"thread 1 done!"<<endl;
	
    return NULL;

}

void* thread_run_2(void* arg)
{
    cout<<"Now in the thread 2"<<endl;
    pthread_cancel(pid[1]);
    sleep(6);				//wait for thread1 to end
    cout<<"thread 2 done!"<<endl;

    return NULL;
}

int main()
{
     pid[0] = pthread_self();
    if (pthread_create(&pid[1],NULL,thread_run_1,NULL) != 0)
    {
        cout<<"error create thread 1"<<endl;
        return -1;
    }
    if (pthread_create(&pid[2],NULL,thread_run_2,NULL) != 0)
    {
        cout<<"error create thread 2"<<endl;
        return -1;
    }

    pthread_join(pid[1], NULL);
    pthread_join(pid[2], NULL);

    cout<<"Main thread done!"<<endl;
    return 0;
}
while the output is:

Now in the thread 1
oldstate is Async
Now in the thread 2
FATAL: exception not rethrown
before testcancel
Aborted

howerver, the output what's I expected is below:
Now in the thread 1
oldstate is Deferred
before testcancel
Now in the thread 2
thread 2 done!
Main thread done!

I am a newbie that i don't know how to modify it, could anybody help me, thx

Last edited by sshh2010_2020; 12-12-2010 at 10:56 PM.
 
Old 12-13-2010, 06:01 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
The main issue is that the cancel type is set to DEFERRED using the wrong function (it should have been setcanceltype, not setcancelstate).

The exact sequence of the output can vary, because you start the two threads at nearly the same time, you cannot guarantee the ordering of these. And the thread 2 may complete before the thread 1 loop finishes and reaches a point where the cancellation will occur (depending on which takes longer, the 1e9 loop or the 6 second delay). Also, the 'before testcancel' occurs after the thread 2 has started (because the 1e9 loop takes quite a while), but I suspect that is what you intended.

Last edited by neonsignal; 12-13-2010 at 06:04 AM.
 
1 members found this post helpful.
Old 12-13-2010, 08:03 AM   #3
sshh2010_2020
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
The main issue is that the cancel type is set to DEFERRED using the wrong function (it should have been setcanceltype, not setcancelstate).

The exact sequence of the output can vary, because you start the two threads at nearly the same time, you cannot guarantee the ordering of these. And the thread 2 may complete before the thread 1 loop finishes and reaches a point where the cancellation will occur (depending on which takes longer, the 1e9 loop or the 6 second delay). Also, the 'before testcancel' occurs after the thread 2 has started (because the 1e9 loop takes quite a while), but I suspect that is what you intended.
hi, thanks for your patience in helping me.
The output is what's I wanted after I change the funtion setcancelstate() to setcanceltype().

howerver, there is another question confuses me, and I don't know why.
That is when I comment out 4 lines in the function thread_run_1(), the output is as same as the first wrong output.
The whole changed code is below:

Code:
#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;

pthread_t pid[3];

void* thread_run_1(void* arg)
{
   //if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
   //    return NULL;

    cout<<"Now in the thread 1"<<endl;

    int state, oldstate;

    state = PTHREAD_CANCEL_DEFERRED;
    pthread_setcanceltype(state, &oldstate);

    cout<<"oldstate is "<<(state == oldstate? "Deferred":"Async")<<endl;

	
    for (int i = 1; i<= 1e9; ++i);  //wait for thread2 to cancel this thread	
    //sleep(2);			//wait for thread2 to cancel this thread
   
    cout<<"before testcancel"<<endl;
    //pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
    pthread_testcancel();
    //pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);

    cout<<"after testcancel"<<endl;
    cout<<"thread 1 done!"<<endl;
	
    return NULL;

}

void* thread_run_2(void* arg)
{
    cout<<"Now in the thread 2"<<endl;
    pthread_cancel(pid[1]);
    sleep(6);				//wait for thread1 to end
    cout<<"thread 2 done!"<<endl;

    return NULL;
}

int main()
{
     pid[0] = pthread_self();
    if (pthread_create(&pid[1],NULL,thread_run_1,NULL) != 0)
    {
        cout<<"error create thread 1"<<endl;
        return -1;
    }
    if (pthread_create(&pid[2],NULL,thread_run_2,NULL) != 0)
    {
        cout<<"error create thread 2"<<endl;
        return -1;
    }

    pthread_join(pid[1], NULL);
    pthread_join(pid[2], NULL);

    cout<<"Main thread done!"<<endl;
    return 0;
}
I just really don't know why we had to use the funtion pthread_setcancelstate() 3 times.
 
Old 12-16-2010, 05:04 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
To reduce the confusion, it would be worth having the two threads write to different streams (since streams are not threadsafe). For trial purposes, perhaps just use cout and cerr. In a real application, you would need to mutex protect the shared stream.

Alternatively, just have a small (eg 1 second) delay before you start the second thread; this will separate the debug output enough that you won't get the crashes caused by using the same stream in two threads. But keep in mind that such code is inherently unsafe.

Quote:
I just really don't know why we had to use the function pthread_setcancelstate() 3 times.
I don't know why the cancel was disabled at all. I thought you wrote this code?

Last edited by neonsignal; 12-16-2010 at 05:06 AM.
 
1 members found this post helpful.
Old 12-16-2010, 08:16 AM   #5
sshh2010_2020
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
To reduce the confusion, it would be worth having the two threads write to different streams (since streams are not threadsafe). For trial purposes, perhaps just use cout and cerr. In a real application, you would need to mutex protect the shared stream.

Alternatively, just have a small (eg 1 second) delay before you start the second thread; this will separate the debug output enough that you won't get the crashes caused by using the same stream in two threads. But keep in mind that such code is inherently unsafe.
I have tried the 2 metheds you mentioned above, however, both of them failed.
Thank you very much all the same for helping me.
I have been learning the Interprocess Communication just for few days, so I guess I won't figure out the problem encountered right row.


Quote:
Originally Posted by neonsignal View Post
I don't know why the cancel was disabled at all. I thought you wrote this code?
The method that use the function pthread_setcancelstate() 3 times doesn't been raised by me. I learn it in the net with the link below:
http://www.linuxquestions.org/questi...thread-633072/

Last edited by sshh2010_2020; 12-17-2010 at 03:36 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pthread_cancel not working lipun4u Programming 5 12-12-2010 11:18 AM
pthread_cancel and system() call poojithas Linux - General 2 06-22-2010 05:03 AM
pthread_cancel throw exception but not always sandeep_talan Programming 1 04-22-2010 03:44 PM
libgcc_s.so.1 must be installed for pthread_cancel to work dlinux Slackware 2 10-11-2008 05:51 PM
pthread_cancel(pthread_t thread) wrong on Redhat 9!!!! tclwp Programming 3 01-14-2005 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration