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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:47:59+00:00 2026-06-13T11:47:59+00:00

I have tried to compile this basic example of C++ atomics & threads though

  • 0

I have tried to compile this basic example of C++ atomics & threads though when I got to compile the main.cpp file gcc throws up a few std lib errors – which seem unrelated to my code.

main.cpp

#include <thread>
#include <atomic>
#include <stdio.h>
#include "randomdelay.h"

using namespace std;

atomic<int> flag;
int sharedValue = 0;

RandomDelay randomDelay1(1, 60101);
RandomDelay randomDelay2(2, 65535);

void IncrementSharedValue10000000Times(RandomDelay& randomDelay)
{
    int count = 0;
    while (count < 10000000)
    {
        randomDelay.doBusyWork();
        int expected = 0;
        if (flag.compare_exchange_strong(expected, 1, memory_order_relaxed))
        {
            // Lock was successful
            sharedValue++;
            flag.store(0, memory_order_relaxed);
            count++;
        }
    }
}

void Thread2Func()
{
    IncrementSharedValue10000000Times(randomDelay2);
}

int main(int argc, char* argv[])
{
    printf("is_lock_free: %s\n", flag.is_lock_free() ? "true" : "false");

    for (;;) {
        sharedValue = 0;
        thread thread2(Thread2Func);
        IncrementSharedValue10000000Times(randomDelay1);
        thread2.join();
        printf("sharedValue=%d\n", sharedValue);
    }

    return 0;
}

Full code I’m using: https://github.com/preshing/AcquireRelease

here’s the gcc error messages:

[lewis@localhost preshing-AcquireRelease-1422872]$ g++ -std=c++0x -pthread main.cpp
/tmp/cc95LElq.o: In function `IncrementSharedValue10000000Times(RandomDelay&)':
main.cpp:(.text+0xdd): undefined reference to `RandomDelay::doBusyWork()'
/tmp/cc95LElq.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x23d): undefined reference to `RandomDelay::RandomDelay(int, int)'
main.cpp:(.text+0x251): undefined reference to `RandomDelay::RandomDelay(int, int)'
collect2: error: ld returned 1 exit status

Here’s the command I use: g++ -std=c++0x -pthread main.cpp

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

    The RandomDelay class seems to be implemented in randomdelay.cpp. You have to compile this file and to link it together with main.cpp. For instance:

    $ g++ -std=c++0x -pthread -o program_name main.cpp randomdelay.cpp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to compile some C++ WIN32 code with GCC through DevC++ (MinGW
Autoconf has gotten me stumped. I have tried modifying the COMPILE in Makefile.in as
Have tried to find solutions for this and can't really come up with anything.
I have tried searching over the internet about this problem but not able to
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I've recently tried operator overloading, and have looked at this stackoverflow page (http://stackoverflow.com/questions/4421706/operator-overloading) about
I there I have this scenario: a) Notebook :Windows Server 2008 R1, Visual basic
The example below defines a basic podtype container class. Using this class a series
I have compiled an Ada program on Ubuntu using GNAT. Afterwards, I tried a
I have tried to go through the jungle (really, PayPal, why don't you weed

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.