Re: Memory Allocation in a Multi-Threaded Environment




"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:3fbo44pe1d25rvuotr1sj58ughh69uaqmv@xxxxxxxxxx
See below...
On Sun, 8 Jun 2008 18:20:16 +0100, "Jeff" <someone@xxxxxxxxxxxxx> wrote:


In general, I believe if you are sharing the same data between two
threads, the design is
already hopeless and needs to be redone. See, for example, my essay "the
best
synchronization is no synchronization", the point being that you should
not create designs
where synchronization is required for correct behavior.

It would not at all be surprising to see heap corruption errors if you are
sharing a
std::vector between two threads. It has nothing to do with "storage
allocation" as such,
but a LOT to do with the fact that none of the STL containers are
"thread-safe" and
therefore should not be used by one thread unless the entire container is
locked against
access by all other threads. This is usually a design error, because it
can mean lengthy
locks, and therefore is usually a Very Bad Idea.

For example, imagine you are iterating over a std::vector of n elements.
While you are
doing the iteration, and modifying the elements, some other thread does a
push_back. Alas,
the first thread, which cleverly has a *pointer* to the array position,
will find itself
writing to unallocated memory, because the push_back could have freed the
old vector
contents after copying to the new vector.


Thanks for your response - it's really appreciated.

As I've already detailed in another response, I do now have a working system
which appears to be both fast and stable. I am however very interested in
your comments especially given your experience in this area - I have just
about enough experience now to know that these problems can be hard.

In my specific case I want to have a streaming thread whose sole purpose is
to download a file as quickly as possible - ie with as few interuptions as
possible. I then have another thread which wants to process this data as
soon as it is available - ie as it is being downloaded. In some cases (eg a
local file system) the download will probably finish before the first bytes
have been processed. In other cases (eg a slow remote file) the processor
will spend a lot of time waiting for bytes to become available.

One thing that confuses me in your response is your comment that sharing the
same data between threads is a bad idea. In the above scenario the two
threads have to share the same data - that's the whole point. Could you
describe a solution to this problem which would not involve the two threads
accessing the same memory location at some point?

Since this specific problem must be encountered fairly often, I imagine
there is already a well optimised stable solution. I'd be really grateful if
someone could point me at it.

Thanks again.

Jeff


.



Relevant Pages

  • Re: lots of aces inevitably spare the overseas expedition
    ... Tom's warehouse might design some unemployed runs. ... You won't imagine me designating in response to your burning ... striking docks. ... I was persuading hosts to territorial Shah, ...
    (sci.crypt)
  • Re: Thread synchronization problem
    ... > the design is supposed to satisfy. ... When I ask to download a Datas object, I download only the list of names, ... When I ask to download a Data object, the information is retrieved from the ... I checked whether the data was empty every time I accessed ...
    (comp.lang.java.programmer)
  • Re: [Rubyinstaller-devel] One-Click Ruby Installer needs a new home, can you help him?
    ... I think that hosting a web site (as opposed a download site) is not ... downloads, but provide a better, simplified and clean access to Ruby ... Simple, clean, friendly design. ...
    (comp.lang.ruby)
  • Re: Questionnaire
    ... define your survey question. ... > What are your thoughts on my design idea and whether I should incorporate ... I am working on a questionnaire entry and analysis database which is ... > Answer Value (i.e. this is the response to the question for that ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Design question - best practice to store survey questions
    ... Your response to the design of tblSurveys ... Re no design for the answers tables: Well, ... So above the line between SURVEY and QUESTION ...
    (comp.databases)