pthread_cleanup_push/pop - cleanup handler problem


 
Thread Tools Search this Thread
Top Forums Programming pthread_cleanup_push/pop - cleanup handler problem
# 1  
Old 12-08-2007
pthread_cleanup_push/pop - cleanup handler problem

Code:
void cleanUp_delete(void * p)
{
	delete p;
	p = NULL;
	cout << "cleanUp_delete" << endl;
}

void connectionReader(void *thread,void*arg)
{	
	connection* c = (connection*) arg;
	pthread_cleanup_push(cleanUp_delete,(void*)c);
	int bytes;
	char *buffer = new (char [512]);
	pthread_cleanup_push(cleanUp_delete,(void*)buffer);
	cout << "listening for " << c->socket << " " << inet_ntoa(c->addr) << endl;
	while(true) {
		if ((bytes = read(c->socket,(void*)buffer,sizeof(char[512]))) == -1){
			cout << "receive() error" << endl; 
			break;
		} 
		if(bytes == 0) break;
		c->buffer.append(buffer);
		cout << "received " << bytes << " bytes from " << inet_ntoa(c->addr) << endl;
		cout << buffer << endl;
		bzero(buffer,sizeof(char[512]));
	}
	cout << "closing connection to " << c->socket << " " << inet_ntoa(c->addr) << endl;
	pthread_cleanup_pop(1);
	pthread_cleanup_pop(1);
	//c->~connection();
	cout << "closed" << endl;
	pthread_exit(NULL);
	return;
}

consider this portion of code. if it wont cout that cleanUp_delete, but if uncomment thqat c->~connection(), it will segfault. so it seems the cleanUp_delete was called, because c is deleted, but why would my message not be printed? the destructor of c, called from the cleanUp handler should cout also, but doesnt. im not sure if those handlers are called in a strange way or not at all. help me out please.
sonicx
# 2  
Old 12-08-2007
i forgot to mention: the handlers should be called when i pthread_cancel the thread running the above while it is waiting for a read().
# 3  
Old 12-09-2007
You need either two different handlers to deal with the different types, or one handler that deals with different types..

Code:
struct mydata
{
  connection *c;
  char *buffer;
};

static void mycleanup(void *arg)
{
struct mydata *data=(struct mydata *)arg;
  delete data->c;
  delete data->buffer;
}

....
{
struct mydata data;

     data.c=(connection *)arg;
     data.buffer=new char[...]

     pthread_cleanup_push(mycleanup,&mydata);

     .....

     pthread_cleanup_pop(1);

     ...
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File Handler of an Output

Hi Guys, I hope anyone could help me on my problem: (perl or shell) I have this command: ns cluster "ns snmp show status" all Then the output is: Command was launched from partition 0. ------------------------------------------------ Executing command in server server6 Event Text... (3 Replies)
Discussion started by: rymnd_12345
3 Replies

2. Programming

problem in doing coding of signal handler

i m unble to execute code of signal handler using a) Wait b) Waitpid (1 Reply)
Discussion started by: madhura
1 Replies

3. Shell Programming and Scripting

Perl Signal Handler

I was working on some Perl code that does signal handling and I came across this one liner and wasn't sure what it was doing. local $SIG{__DIE__} = sub {$! = 2; die $_;}; I think the first part of the anonymous subroutine is setting $! to 2, but I am not sure what the second part is doing. ... (1 Reply)
Discussion started by: SFNYC
1 Replies

4. Programming

Signal Handler Hangs

Hi, I have a problem with signal handler algorithm in linux. My code is hanging ( It is continuously looping inside the signal handler) . I am pasting my code here... Please provide me some help regarding this. I googled many places and wrote this code.. but doesnt seem to be working without... (6 Replies)
Discussion started by: sree_ec
6 Replies

5. UNIX for Dummies Questions & Answers

Doubt with irq handler.......

Hello, I have develop a driver for my hardware and now, I need to handle a IRQ but I does not work. As I can understand, to handle a irq, it is necessary to make a request_irq(). If the return value is zero, ok, no problem to handle irq. Here is a easy example of my driver: #include... (8 Replies)
Discussion started by: webquinty
8 Replies

6. Shell Programming and Scripting

XML Handler in perl

Hi there, I'm newby in perl and XML. I can read and parse Xml with XML-Node upper XML::Parser, but how can I create XML tags and pack my individual data in it then send through socket. PLZ lead me :) Meanwhile what is your opinion about XML Writer library? Thanks in Advance. (2 Replies)
Discussion started by: Zaxon
2 Replies

7. Programming

Problem with signal handler and interrupted system call

Hi, I have a daq program that runs in an infinite loop until it receives SIGINT. A handler catches the signal and sets a flag to stop the while loop. After the loop some things have to be cleaned up. The problem is that I want my main while loop to wait until the next full second begins, to... (2 Replies)
Discussion started by: soeckel
2 Replies

8. Programming

signal handler problems

Hey guys, I am trying to write a little shell, and was writing a signal handler to handle SIGINT (I am using 'stty intr ^C' and using ctrl-C to give SIGINT). I wrote this signal handler: void handle_sigint() { write(2,"handling sigint\n",16); write(1,"\nshell% ",8); } ... (4 Replies)
Discussion started by: blowtorch
4 Replies

9. Shell Programming and Scripting

File Handler in TCL

Hai , I Have Some x.txt file in which has the following data x.txt HI, How Are u r u fine /home/Sanju/samp.html /root/Sanju/design/sample now in tcl i have the following script set fp while { >= 0 } { puts $line ... (1 Reply)
Discussion started by: sanjustudy
1 Replies

10. Programming

signal handler for SIGCHLD

Hi, I have an c++ application which uses the function fork and execvp(). The parent does not wait until the child ends. The parents just creates children and let them do their stuff. You can see the parent program as a batch-manager. I have added a SIGCHLD handler to the program: void... (3 Replies)
Discussion started by: jens
3 Replies
Login or Register to Ask a Question