create a list of threads in C

Posted by Hristo on Stack Overflow See other posts from Stack Overflow or by Hristo
Published on 2010-04-21T16:19:35Z Indexed on 2010/04/21 16:23 UTC
Read the original article Hit count: 336

Filed under:
|
|

My implementation (in C) isn't working... the first node is always NULL so I can't free the memory. I'm doing:

thread_node_t *thread_list_head = NULL; // done in the beginning of the program

...

// later on in the program
thread_node_t *thread = (thread_node_t*) malloc(sizeof(thread_node_t));
pthread_create(&(thread->thread), NULL, client_thread, &csFD);
thread->next = thread_list_head;
thread_list_head = thread;

So when I try to free this memory, thread_list_head is NULL.

while (thread_list_head != NULL) {
    thread_node_t *temp = thread_list_head;
    thread_list_head = thread_list_head->next;
    free(temp);
    temp = NULL;
}

Any advice on how to fix this or just a new way to create this list that would work better?

Thanks, Hristo

© Stack Overflow or respective owner

Related posts about thread

Related posts about list