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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:02:06+00:00 2026-05-27T15:02:06+00:00

So I discovered that gcc already had a working implementation of std::atomic so I

  • 0

So I discovered that gcc already had a working implementation of std::atomic so I wanted to try it out, here is my example using boost thread 1.48 and std::atomic:

#include <iostream>
#define BOOST_THREAD_USE_LIB
#include <boost/thread.hpp>
#include <atomic>
#include <string>

using namespace std;
using namespace boost;

class Server
{
    public:
    atomic<bool> running;
    thread main_server_thread;


    void func1()
    {
        while(running)
        {
            //do stuff
        }
    }
    void func2()
    {
        while(running)
        {
            //do stuff
        }
    }
    void func3()
    {
        while(running)
        {
            //do stuff
        }
    }
    void func4()
    {
        string input;
        while(running)
        {
            //do stuff
            input.clear();
            getline(cin,input);
            if(input=="quit")running=false;
        }
    }

    void main_function()
    {
        thread thrd1(bind(&Server::func1,this));
        thread thrd2(bind(&Server::func2,this));
        thread thrd3(bind(&Server::func3,this));
        thread thrd4(bind(&Server::func4,this));

        while(running)
        {
            //do stuff
        }
        thrd1.join();
        thrd2.join();
        thrd3.join();
        thrd4.join();
    }

    Server()
    {
        running=true;
        main_server_thread = thread(&Server::main_function,this);
    }
};



int main()
{
    Server* serv = new Server();
    serv->main_server_thread.join();
    return 0;
}

Now while running remains true all is fine but when the user inputs quit and running is set to false some threads end and some don’t. This happens with and without optimizations. Is this working as intended? In my understanding of atomics the reads shouldn’t conflict with the one write so the threads should see the running=false at some point.

EDIT:
Disassembly of func1:

void func1()
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 18                sub    $0x18,%esp
        {
            while(running)
   6:   90                      nop
   7:   8b 45 08                mov    0x8(%ebp),%eax
   a:   89 04 24                mov    %eax,(%esp)
   d:   e8 00 00 00 00          call   12 <__ZN6Server5func1Ev+0x12>
  12:   84 c0                   test   %al,%al
  14:   75 f1                   jne    7 <__ZN6Server5func1Ev+0x7>
            {
                //do stuff
            }
        }
  16:   c9                      leave  
  17:   c3                      ret    
  • 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-27T15:02:06+00:00Added an answer on May 27, 2026 at 3:02 pm

    As the disassembly shows, this program, when compiled with mingw, does not treat running as an atomic: it is loaded with a regular mov 0x8(%ebp),%eax without synchronization.

    Compare to the output of gcc-4.6.2 on linux/x86_64:

    _ZN6Server5func1Ev:      // Server::func1()
        jmp     .L78
    .L79:
        // do stuff
    .L78:
        mfence
        movzbl  (%rdi), %eax // load from `running`
        mfence
        testb   %al, %al
        jne     .L79
        ret
    

    Either mingw doesn’t support that yet, or something is missing in its configuration.

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

Sidebar

Related Questions

I discovered that one of the tables of a legacy db I'm working on
Through profiling I've discovered that the sprintf here takes a long time. Is there
I'm using python 2.6.4 and discovered that I can't use gzip with subprocess the
I discovered that if I try to access a field in a record, e.g.:
I recently discovered that 2 == [2] in JavaScript. As it turns out, this
I've just discovered that using <div /> instead of <div></div> completely destroys my page
I just discovered that some parts of the code I am working on incorrectly
Recently I've been playing around with cross compiling using GCC and discovered what seems
I have discovered that there are still a fair number of drivers out there
I discovered that it is possible to extract the hard-coded strings from a binary.

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.