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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:07:42+00:00 2026-06-06T16:07:42+00:00

Consider the following szenario: 2 different network-ports via boost::asio each in its own thread

  • 0

Consider the following szenario:

  • 2 different network-ports via boost::asio each in its own thread
  • 1 port is receiving and processing data – class DataConnection wrapped in a std::thread
  • 1 port is for sending statistics class StatConnection also wrapped in a std::thread

For counting connections (and other small data pieces) my idea was to use a static variable inside a namespace like:

#include <atomic>

namespace app {
 namespace status {
   static std::atomic<long> counter = 0; 
 }
}

This works fine for the DataConnection class. Here I increment counter in the c’tor and see the value increments.

But counter in my StatConnection class is always 0

Why can this happen?

I’ve tried some alternatives:

  • exchanging std::atomic<long> for static volatile long: Did not made a difference.
  • using the namespace without static keyword.

Then I got linker errors:

multiple definition of `app::status::searchtime'
./src/status/Status.o:/[...]/include/status/Status.hpp:16: first defined here
[...]

So why is the value of count different between threads?

  • 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-06T16:07:43+00:00Added an answer on June 6, 2026 at 4:07 pm

    static in namespace scope introduces internal linkage, so each translation unit will have its own copy of counter – quite the opposite of what you actually want!

    Use extern instead, in the header:

    //foo.h:
    #include <atomic>
    
    namespace app {
        namespace status {
            extern std::atomic<long> counter;
        }
    }
    

    Then define the variable in one translation unit:

    //foo.cpp:
    #include "foo.h"
    
    namespace app {
        namespace status {
            std::atomic<long> counter{0L};
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following scenario:- A bookstore website has number of books and each book
Consider the following scenario: I own www.a.com , and in my index page I
Consider the following scenario: SCO A is launched, SCO A sets its cmi.exit =
Consider the following scenario: 3 EC2 instances located in: US-WEST Ireland Tokyo Each instance
Consider the following scenario: There exists a globally accessible variable F . Thread A
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following scenario: /** * A sample interface. */ public interface MyInterface {
Consider the following scenario We have a simple database that involves two entities: user
Consider the following scenario . I have an array of numbers: [ 1,2,3,4 ]
Let's consider the following scenario: a function which can generate code colors from white

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.