From: Randi Botse <nightdecoder@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: thread: reentrant question
Date: Thu, 24 Dec 2009 13:41:30 -0500 [thread overview]
Message-ID: <34e1241d0912241041v60c031a2kc31fdb3b4ebbd317@mail.gmail.com> (raw)
Hi all,
I'm now learning thread reentrancy on Linux, so i write simple codes
for demonstration:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *func(void *data)
{
char *ptr;
ptr = malloc(8);
printf("thread%i -> ptr at: %p\n", *(int*) data, ptr);
free(ptr);
return NULL;
}
#define NTHREAD 5
int main(void)
{
pthread_t thread[NTHREAD];
int i;
for (i = 0; i < NTHREAD; i++)
if (pthread_create(&thread[i], NULL, func, (void*) &i))
return -1;
for (i = 0; i < NTHREAD; i++)
pthread_join(thread[i], NULL);
return 0;
}
I know the func() function is not reentrant, worst printf() debugging
told me that's each thread pointer to character (ptr) may has same
address, like this output:
thread1 -> ptr at: 0x9a80128
thread1 -> ptr at: 0x9a80138
thread2 -> ptr at: 0x9a801d8
thread4 -> ptr at: 0x9a801d8
thread4 -> ptr at: 0x9a801d8
I try to modify the func() by not free ptr to be:
void *func(void *data)
{
char *ptr;
ptr = malloc(8);
printf("thread%i -> ptr at: %p\n", *(int*) data, ptr);
/* free(ptr); */
return NULL;
}
And the result is i always got different address of each thread's ptr, such as:
thread0 -> ptr at: 0x8db2098
thread1 -> ptr at: 0x8db2138
thread2 -> ptr at: 0x8db21d8
thread3 -> ptr at: 0x8db2278
thread4 -> ptr at: 0x8db2318
Can you explain me why this happen? how about my printf() debugging
method? it's works for this demonstration?
Thanks before.
- Randi
next reply other threads:[~2009-12-24 18:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-12-24 18:41 Randi Botse [this message]
2009-12-24 19:30 ` thread: reentrant question vinit dhatrak
2009-12-26 5:32 ` Randi Botse
2009-12-26 10:42 ` Michał Nazarewicz
2009-12-26 21:23 ` vinit dhatrak
2009-12-27 13:12 ` Randi Botse
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=34e1241d0912241041v60c031a2kc31fdb3b4ebbd317@mail.gmail.com \
--to=nightdecoder@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).