LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-13-2005, 03:37 AM   #1
tclwp
Member
 
Registered: Jan 2005
Posts: 37

Rep: Reputation: 15
Question pthread_cancel(pthread_t thread) wrong on Redhat 9!!!!




Hello everybody , I had bought <<Linux Programming Unleashed Edition
2>> . The day before yesterday I compiled thrdcancel.c (charpter 14) with gcc 3.2.2 on Redhat 9 but found the program didn't work as we guess. I did't change a word in thrdcancel.c ,
It's output is:

task1 count: 0
task2 count: 0
task2 count: 1
task2 count: 2
task2 count: 3
task2 count: 4
task1 count: 1
total iterations: 6

but it's output should be:
task1 count: 0
task2 count: 0
task2 count: 1
task2 count: 2
task2 count: 3
task2 count: 4
task1 count: 1
task1 count: 2
task1 count: 3
task1 count: 4
total iterations: 10

Here is source code:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

void task1(int *counter);
void task2(int *counter);
void cleanup(int counter1,int counter2);


int g1 = 0;
int g2 = 0;

int main(int argc, char *argv[])
{
pthread_t thrd1,thrd2;
int ret;

//create the first thread
ret = pthread_create(&thrd1,NULL,(void*)task1,(void*)&g1);
if(ret)
{
perror("pthread_create:task1");
exit(EXIT_FAILURE);
}

//create the second thread
ret = pthread_create(&thrd2,NULL,(void*)task2,(void*)&g2);
if(ret)
{
perror("pthread_create:task2");
exit(EXIT_FAILURE);
}

pthread_join(thrd2,NULL);
//cancel the first thread
pthread_cancel(thrd1);
pthread_join(thrd1,NULL);

cleanup(g1,g2);

exit(EXIT_SUCCESS);


}

void task1(int *counter)
{
//Disable thread cancellaion
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
while(*counter < 5)
{

printf("task1 count: %d\n",*counter);
(*counter)++;
sleep(1);
}
//Enable thread cancellation
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);

}

void task2(int *counter)
{
while(*counter < 5)
{
printf("task2 count: %d\n",*counter);
(*counter)++;

}

}


void cleanup(int counter1,int counter2)
{
printf("total iterations: %d\n", counter1 + counter2);
}



Obviously thread 1(task1) was killed even after pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); called.
It should not be killed.

I don't know why the code don't work on Redhat 9. Can you tell me how to rewrite the code to let it work as we hope ?

Thanks in advance.
 
Old 01-13-2005, 03:33 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
In my system it works correctly. Check what does pthead_setcancelstate return. It should be 0. If it's not, there was an error.
 
Old 01-13-2005, 07:18 PM   #3
tclwp
Member
 
Registered: Jan 2005
Posts: 37

Original Poster
Rep: Reputation: 15
Unfortunately,
pthread_setcancelstate() does return 0
pthread_cancel() return 0 too
 
Old 01-14-2005, 07:08 AM   #4
tclwp
Member
 
Registered: Jan 2005
Posts: 37

Original Poster
Rep: Reputation: 15
update glibc to version 2.3.3
all is ok
 
  


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
Redhat 9.0 wrong disk drhackler Linux - Software 6 05-13-2005 04:06 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM
Mandrake vs. RedHat thread adas Linux - Distributions 46 10-17-2003 07:57 AM
Multi-thread debugging with gdb on RedHat 9.0 Rodney Riggs Linux - Software 0 09-23-2003 04:34 PM

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

All times are GMT -5. The time now is 09:57 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