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 6320039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:54:11+00:00 2026-05-24T15:54:11+00:00

I’m aware, that I need to use mutex, when I perform operations on single

  • 0

I’m aware, that I need to use mutex, when I perform operations on single STL container inside multiple threads. However I want to know if there are any exceptions from this rule. Please consider simplified scenario I’m trying to implement.

I have multiple threads adding elements to container and operation is surrounded with mutex lock/unlock. Then threads notify somehow (e.g. using eventfd on linux) single thread dedicated to dispatch elements in this container. What I want to do is to access first element in container without using mutex. Sample code based on deque but note that I ca use any container with queue capability:

std::mutex     locker;
std:deque<int> int_queue;
int            fd; // eventfd
eventfd_t      buffer;
bool           some_condition;

Thread 1, 2, 3, etc.

locker.lock ();
int_queue.push_back (1);
locker.unlock ();

eventfd_write (fd, 1);

Thread dedicated to dispatch elements:

while (true)
{
    bool some_condition (true);

    locker.lock ();
    if (int_quque.empty () == false)
    {
        locker.unlock ();
    }
    else
    {
        locker.unlock ();
        eventfd_read (fd, &buffer);
    }

    while (some_condition)
    {
        int& data (int_queue.front ());

        some_condition = some_operation (data); // [1]
    }

    locker.lock ();
    int_queue.pop ();
    locker.unlock ();
}

[1] I will do some_operation() on signle element many times, that’s why I want to avoid mutex locking here. It’s to expensive.

I want to know if this code can lead to any synchronisation problems or 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. Editorial Team
    Editorial Team
    2026-05-24T15:54:12+00:00Added an answer on May 24, 2026 at 3:54 pm

    What you need is reference stability. I.e. you can use containers this way if the reference to the first element is not invalidated when the container is push_back’d. And even then, you’ll want to obtain the reference to the front element under the lock.

    I’m more familiar with std::condition_variable for the event notification, so I’ll use that:

    #include <mutex>
    #include <condition_variable>
    #include <deque>
    
    std::mutex              locker;
    std::deque<int>         int_queue;
    std::condition_variable cv;
    
    void thread_1_2_3()
    {
        // use lock_guard instead of explicit lock/unlock
        //    for exception safety
        std::lock_guard<std::mutex> lk(locker);
        int_queue_.push_back(1);
        cv.notify_one();
    }
    
    void dispatch()
    {
        while (true)
        {
            bool some_condition = true;
            std::unique_lock<std::mutex> lk(locker);
            while (int_queue.empty())
                cv.wait(lk);
            // get reference to front under lock
            int& data = int_queue.front();
            lk.unlock();
            // now use the reference without worry
            while (some_condition)
                some_condition = some_operation(data);
            lk.lock();
            int_queue.pop_front();
        }
    }
    

    23.3.3.4 [deque.modifiers] says this about push_back:

    An insertion at either end of the deque invalidates all the iterators
    to the deque, but has no effect on the validity of references to
    elements of the deque.

    That is the key to allowing you to hang onto that reference outside of the lock. If thread_1_2_3 starts inserting or erasing in the middle, then you can no longer hang on to this reference.

    You can’t use a vector this way. But you could use a list this way. Check each container you want to use this way for reference stability.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.