The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
performing cleanup when a job finishes ChicagoBlues Shell Programming and Scripting 4 03-06-2008 12:41 PM
awk/sed/ksh script to cleanup /etc/group file pdtak Shell Programming and Scripting 6 02-28-2008 03:33 AM
Help with cleanup whdr02 Shell Programming and Scripting 2 01-25-2008 03:44 PM
Login ID cleanup MILLERJ62 AIX 1 05-12-2006 05:20 AM
sendmail cleanup thomi39 UNIX for Dummies Questions & Answers 1 02-23-2006 09:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-08-2007
sonicx sonicx is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 9
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 (permalink)  
Old 12-08-2007
sonicx sonicx is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 9
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 (permalink)  
Old 12-09-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
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);

     ...
}

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0