ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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
I don't know why the cancel was disabled at all. I thought you wrote this code?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.