in reply to Re^7: memory leaks with threads
in thread memory leaks with threads
#include <iostream> #include <cstdlib> using namespace std; #include <pthread.h> #include <unistd.h> #include <sys/select.h> #include <sys/time.h> using namespace std; static int threadscount = 0; pthread_mutex_t threadscount_mutex = PTHREAD_MUTEX_INITIALIZER; void* thread( void* data ){ pthread_mutex_lock( &threadscount_mutex ); threadscount--; //cout << threadscount << endl; pthread_mutex_unlock( &threadscount_mutex ); pthread_exit(0); } int main(int argc, char *argv[]){ int count = 0; int a; struct timeval timeout; while (1){ for ( int a = 1; a <= 10; a++ ){ pthread_mutex_lock( &threadscount_mutex ); threadscount++; pthread_mutex_unlock( &threadscount_mutex ); pthread_t p_thread; /* thread's structu +re */ pthread_create(&p_thread, 0, thread, (void*)&a +); pthread_detach( p_thread ); count ++; } cout << "count: " << count << endl; int c; do { timeout.tv_usec = 100; timeout.tv_sec = 0; select( 0, 0, 0, 0, &timeout ); pthread_mutex_lock( &threadscount_mutex ); c = threadscount; pthread_mutex_unlock( &threadscount_mutex ); } while ( c > 0); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^9: memory leaks with threads
by BrowserUk (Patriarch) on Jul 09, 2007 at 23:33 UTC | |
by misc (Friar) on Jul 10, 2007 at 13:34 UTC | |
by BrowserUk (Patriarch) on Jul 10, 2007 at 13:57 UTC |