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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:57:09+00:00 2026-06-13T04:57:09+00:00

Consider the following code in a multithread program: QString target = remaining.first(); // remaining

  • 0

Consider the following code in a multithread program:

    QString target = remaining.first(); // remaining is a QVector<QString> class
    remaining.pop_front();

Would it be safe? Looks like multiple thread may use the same “target” simultaneously. Or what’s the safe way to retrieve + erase the first value?

  • 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-13T04:57:11+00:00Added an answer on June 13, 2026 at 4:57 am

    Without a mutex protecting that code, no, it’s not at all safe.

    I don’t know QVector in detail but I believe it’s OK for two threads to both do:

    QString target = remaining.first();
    

    This simply copies an element of the vector, so each thread has its own QString object called target and they are independent objects (behind the scenes they use implicit sharing so are not independent, but you should be able to treat them as independent)

    But this line modifies the QVector:

    remaining.pop_front();
    

    This means two threads modify the same object without any synchronisation. If the first thread is still accessing the vector by calling remaining.first() when the second thread calls pop_front() then there is a data race, with undefined behaviour.

    Similarly, if both threads call pop_front() concurrently they will both try to remove the first element, what happens there is completely unpredictable. You might erase one element, or two, or none, or crash the entire program immediately. As another possibility, consider what happens if the vector only has one element. Both threads check it’s not empty, copy the first() element, then call pop_front(), which tries to remove two elements when there’s only one. You’re program is broken.

    The safe way to do it is protect the code with a mutex, where mutex is some global or otherwise shared variable that is visible to both threads:

    QString target;
    {
      QMutexLocker locker(&mutex);
      if (!remaining.empty())
      {
        target = remaining.first();
        remaining.pop_front();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following code, I want to make it a thread safe class, so that
Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
Consider the following code: class Vehicle { /** * Create a new instance of
Consider the following code: class C { public int A { get; set; }
Consider the following code in a DLL: public class ReceivingClass { private Assembly myAssembly;
Consider the following code: class A { private $a; function f() { A::a =
Consider the following code: [DataContract] class QObject { public QObject() { } [DataMember(Name =
consider the following code: public class SynchronizedCounter extends Thread { private int c =

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.