UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » pthread question

#1 2010-01-27 11:27 PM

ice9_us
Member
Registered: 2010-01-27
Posts: 4

Re: pthread question

I was workingn on trying to make a threaded process...
i ran into some issues and found doing the following doesn't work exactly as the docs say..

i have tried the below as detached and joinable...
I basically want the stuff below to exit like a fork() child process..
i know below i have a memory leak.. i want the child thread to free that memory when it goes away.. so any open FDs or allocated memory will go away... i can't make that happen using pthread.. anyone have any ideas?


---------------------------------------------------

void *mChild();


main() {

pid_t pid[100];
int a;
int status;
pid_t pidt[1024];


pthread_t thread[1024];
pthread_attr_t thread_attr;
pthread_attr_init( &thread_attr);
pthread_attr_setdetachstate( &thread_attr, PTHREAD_CREATE_JOINABLE);
while (1) {
        for ( a = 0 ; a < 100; a++ ) {
                pthread_create( &thread[a], &thread_attr, (void *) mChild, NULL );
                pthread_join(thread[a], NULL);
               // pthread_detach(thread[a]);
        }
}

}

void *mChild() {
        char *a=malloc(8192);
        /*
        free(a);
        */
        pthread_exit(NULL);
}

Offline

#2 2010-01-28 03:26 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: pthread question

Offline

#3 2010-01-28 04:27 PM

ice9_us
Member
Registered: 2010-01-27
Posts: 4

Re: pthread question

when i use fork(); i get to many fork processes....
Can you give more detail on the thread cleanup?
Each thread i make works as its own guy.. using its own resources...
pthread_create(); is better to use than fork because i don't make a copy of the address space.. so resource utilz is far less...

Offline

#4 2010-01-28 09:34 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: pthread question

Offline

  • Index
  • » C
  • » pthread question

Board footer

Powered by FluxBB