Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #146389
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: realloc() and POSIX threads |
| Date | 2019-09-28 03:53 -0700 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <qmne2g$vdj$1@gioia.aioe.org> (permalink) |
| References | <qmlv0b$bt0$1@news.albasani.net> |
On 9/27/2019 2:29 PM, Doug Weiman wrote: > My question may not belong in this group - my apologies if that > is the case. > > I have some application that allocates memory with malloc(), and > then uses the resulting pointer as an argument to a POSIX thread that is > launched immediately after. While the thread is doing its thing with the > memory pointed to by this pointer, the application augments that pointer > with realloc(). > > My understanding is that realloc() is no guaranteed to keep the > original pointer. That is, if necessary, realloc() will obtain a > different pointer to a memory region with enough space, and copy here the > contents of the memory region pointed to by the original pointer, finally > returning thee new pointer to the caller. > > This is, of course, potentially disastrous to the thread above, > which is still operating on a memory area now deallocated. I mean, you > may be lucky and the area will still be usable, but sooner or later the > whole thing will blow up in yor face. > > Is there a way to implement the scenario above (memory > allocation, thread creation, memory reallocation) safely, so that the > thread can be guaranteed to still have access to valid memory? One could > use thread mutexes, but that would effectively serialize things. > > Any other suggestions? Or is this the kind of thing that one just > ought not to do, period? For a number of reasons, getting the thread > itself to allocate the memory is not an option. > Never call realloc on live memory, where other threads can be using it. Instead, wait for the memory to go into a quiescent state, then you can free it, and call realloc.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
realloc() and POSIX threads Doug Weiman <no@nyet.net> - 2019-09-27 21:29 +0000
Re: realloc() and POSIX threads Robert Wessel <robertwessel2@yahoo.com> - 2019-09-27 17:11 -0500
Re: realloc() and POSIX threads Florian Weimer <fw@deneb.enyo.de> - 2019-09-28 09:27 +0200
Re: realloc() and POSIX threads Melzzzzz <Melzzzzz@zzzzz.com> - 2019-09-28 08:42 +0000
Re: realloc() and POSIX threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-28 03:53 -0700
Re: realloc() and POSIX threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-28 03:55 -0700
Re: realloc() and POSIX threads Richard Damon <Richard@Damon-Family.org> - 2019-09-28 08:29 -0400
Re: realloc() and POSIX threads Doug Weiman <no@nyet.net> - 2019-09-28 15:06 +0000
Re: realloc() and POSIX threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-28 15:19 -0700
Re: realloc() and POSIX threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-28 15:23 -0700
csiph-web