The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
HP-UX memory usage allocation dehuang83 HP-UX 3 06-02-2007 03:40 AM
HP-UX memory usage allocation dehuang83 UNIX for Dummies Questions & Answers 1 05-03-2007 01:40 AM
tar: Memory allocation failed gfhgfnhhn UNIX for Dummies Questions & Answers 1 03-05-2007 11:23 AM
Static variables memory allocation nathanmca High Level Programming 8 06-15-2005 12:57 AM
memory allocation sagar UNIX for Dummies Questions & Answers 1 01-05-2002 11:53 PM

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

Join Date: Apr 2005
Posts: 5
threads and memory allocation

Hello!
First of all, forgive me for bad English.

When I starts new thread (pthread_create), system allocates some memory for it (for example, for thread's stack). I wonder when does it deallocate this memory? The problem is that I have a program which sometimes creates new threads and sometimes deletes it. And memory used by my program always increases and never decreases (I use top utility to watch for my program behaviour). And I wonder is it my bug or standard behaviour. It seems my program stops threads which are not needed anymore, but memory doesn't decrease and after some time pthread_create says that it can't create new thread.

I will really appreciate your help!

P.S. The system is Solaris 5.10, if it matters.

Last edited by prankster; 12-14-2005 at 10:22 AM..
  #2 (permalink)  
Old 12-14-2005
krhamidou's Avatar
krhamidou krhamidou is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 27
do you use pthread_exit to terminate your threads ?
  #3 (permalink)  
Old 12-14-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
Some memory mgt strategies keep all the memory that brk() gets for the process.

Are you calling pthread_cleanup_push() & pthread_cleanup_pop() ? Under some circumstances: if the (void *) arg == 0 nothing will happen. ie., you leave junk behind.
  #4 (permalink)  
Old 12-14-2005
prankster prankster is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 5
1. Yes, I use cleanup_push and pop. Here is a part of my code:
Code:
void thread_cleanup(void* r) {
	ARunnable* runnable = static_cast<ARunnable*>(r);
	runnable->onClose();
	delete runnable;
}

void* thread_function(void* r) {
	CThread* th = static_cast<CThread*>(r);
	ARunnable* runnable = th->target();	
	pthread_cleanup_push(thread_cleanup, static_cast<void*>(runnable));
	try {
		runnable->run();
	}
	catch (...) {
		cerr << "error" << endl;
	}
	pthread_cleanup_pop(1);          // 0
	pthread_detach(th->thread());  // 1
	pthread_exit(NULL);                 // 2

	return NULL;
}

bool CThread::start() {
	int e = 0;
	
	if (0 != (e = pthread_create(&_thread, NULL, thread_function, static_cast<void*>(this)))) {
		return false;
	}

	return true;
}
As you see, I've tried to make a little wrapper for pthread_* routines. I've added lines labelled 1 and 2 because I thought it would make threads to deallocate memory. But it doesn't work.

Also I have some misunderstanding about pthread_cleanup_push and pop technics. Does the system return control to the thread_function after calling thread_cleanup, if thread_cleanup is called not from the line 0?
Sponsored Links
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:13 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