Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 72201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:00:27+00:00 2026-05-10T20:00:27+00:00

I am writing code in VS2005 using its STL. I have one UI thread

  • 0

I am writing code in VS2005 using its STL. I have one UI thread to read a vector, and a work thread to write to a vector. I use ::boost::shared_ptr as vector element.

vector<shared_ptr<Class>> vec; 

but I find, if I manipulate the vec in both thread in the same time(I can guarantee they do not visit the same area, UI Thread always read the area that has the information)

vec.clear() seem can not release the resource. problem happend in shared_ptr, it can not release its resource.

What is the problem? Does it because when the vector reach its order capacity, it reallocates in memory, then the original part is invalidated.

As far as I know when reallocating, iterator will be invalid, why some problem also happened when I used vec[i]. //———————————————–

What kinds of lock is needed? I mean: If the vector’s element is a shared_ptr, when a thread A get the point smart_p, the other thread B will wait till A finishes the operation on smart_p right? Or just simply add lock when thread is trying to read the point, when the read opeation is finished, thread B can continu to do something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:00:28+00:00Added an answer on May 10, 2026 at 8:00 pm

    When you’re accessing the same resource from more than one thread, locking is necessary. If you don’t, you have all sorts of strange behaviour, like you’re seeing.

    Since you’re using Boost, an easy way to use locking is to use the Boost.Thread library. The best kind of locks you can use for this scenario are reader/writer locks; they’re called shared_mutex in Boost.Thread.

    But yes, what you’re seeing is essentially undefined behaviour, due to the lack of synchronisation between the threads. Hope this helps!

    Edit to answer OP’s second question: You should use a reader lock when reading the smart pointer out of the vector, and a writer lock when writing or adding an item to the vector (so, the mutex is for the vector only). If multiple threads will be accessing the pointed-to object (i.e., what the smart pointer points to), then separate locks should be set up for them. In that case, you’re better off putting a mutex object in the object class as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 162k
  • Answers 162k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a complicated situation, as it seems the "{http://xml.apache.org/xalan}omit-meta-tag"… May 12, 2026 at 12:01 pm
  • Editorial Team
    Editorial Team added an answer Try sending a random hash as parameter in the url,… May 12, 2026 at 12:01 pm
  • Editorial Team
    Editorial Team added an answer http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player.html explains Flash 9's h.264 and aac support. Presumably Flash… May 12, 2026 at 12:01 pm

Related Questions

I am writing a DLL with mixed C/C++ code. I want to specify the
Here's something I know is probably possible but I've never managed to do In
i seem to write this code over and over again and wanted to see
I have very little experience with ASP.NET and I am doing some self-training before

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.