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

  • SEARCH
  • Home
  • 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 8171583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:30:22+00:00 2026-06-06T21:30:22+00:00

Each thread may insert an object to the container (at most once) using ‘insert’

  • 0

Each thread may insert an object to the container (at most once) using ‘insert’ function.
Hereafter, the thread may try to access this object using ‘get’ function.
Therefore, there is no race between ‘insert’ and ‘get’ when used by the same thread.
However, a different thread may try to insert its own object while another thread has called ‘get’

I need a container where this situation does not need any synchronization method.

The number of threads may vary dramatically between executions.

class Object;
class Container<Object>;

Container<Object> g_container;

void insert(int threadId)
{
    ScopedLock<Mutex> lock(insertMutex);
    Object obj;
    g_container[threadId] = obj;
}

Object get(int threadId)
{
    return g_container[threadId];
}
  • 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. Editorial Team
    Editorial Team
    2026-06-06T21:30:23+00:00Added an answer on June 6, 2026 at 9:30 pm

    You can use a vector of container pointers. Each thread manages their own container instance, and registers it with the array of container pointers.

    template <typename T>
    struct Container {
        Mutex insertMutex;
        ContainerType<T> container;
        int index;
        void insert (T &obj) {
            ScopedLock<Mutex> lock(insertMutex);
            container.insert(obj);
        }
        T get () {
            return *container.begin();
        }
        void register_container () {
            if (index != 0) return;
            if (counter == MAX_THREADS) throw 0;
            index = ++counter;
            ScopedLock<Mutex> lock(registerMutex);
            containers.push_back(this);
        }
        static std::vector<Container *> containers;
        static Atomic<int> counter;
        static Mutex registerMutex;
    };
    
    template <typename T>
    std::vector<Container<T> *> Container<T>::containers;
    
    template <typename T>
    Atomic<int> Container<T>::counter;
    
    template <typename T>
    Mutex Container<T>::registerMutex;
    

    Now, you can iterate over the Container<T>::containers to access all containers.

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

Sidebar

Related Questions

I am using ACE threads and need each thread to have its own int
Many threads have access to summary . Each thread will have an unique key
Since each thread has its own stack, its private data can be put on
I found that each thread still has its own registers. Also has its own
Assume I have one threadpool and each thread is running following method: void runMe(HashMap
As we all know in case of multiple threads each thread maintains it's separate
In my application, each thread handle a Form, and its events shall executed within
Does ASIHTTP support multi thread? If sure, I hope each thread link to aUIProgressbar,
Is there a way to see how many context switches each thread generates? (both
I'm writing a multithreaded Java program where each thread potentially needs its standard output

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.