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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:48:01+00:00 2026-06-15T19:48:01+00:00

Is it possible to safely move unique_ptr with c++11 atomic operations? Currently I have

  • 0

Is it possible to safely move unique_ptr with c++11 atomic operations?

Currently I have a code like this

std::unique_ptr<SyncToken> DataManager::borrowSyncToken()
{
    std::unique_lock<std::mutex> syncTokenLock(syncTokenMutex);
    return std::move(syncToken);
}

I am wondering whether there is some more elegant way, like simply declaring:

std::atomic<std::unique_ptr<SyncToken>> syncToken;

and avoiding the need of mutex. Or possibly I don’t need to care about the lock here at all and std::move is already atomic?

After research I made so far it seems to me:

  • the std::move itself is not atomic and needs to have some synchronization around otherwise 2 threads calling my method concurrently may end up with 2 copies of some undefined pointers.
  • the std::atomic declaration compiles for me, but I don’t know how to initialize it and and make the move.
  • 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-15T19:48:02+00:00Added an answer on June 15, 2026 at 7:48 pm

    No, this is not possible.

    The value T which you pass to std::atomic needs to be trivially copyable, which std::unique_ptris not. Operations like std::atomic::load or std::atomic::store take T objects by value.

    Packing something in a std::atomic also doesn’t make operations from the value atomic.

    When using std::unique_ptr in an atomic context, you have to think about the fact that you might have problems when it comes to managing resources. You never know how many threads still refer to your data, this problem can be solved with a std::shared_ptr which uses atomic reference counting. (You need to check whether it’s really atomic by using the std::atomic_is_lock_free function. )

    One thing I was also stumbling upon when looking in your code is the intent of the borrowSyncToken function. It’s called borrow but you pass ownership of the token to the caller by moving out the std::unique_ptr, how is the ownership passed back and what do other threads get when the DataManager currently doesn’t own the token?

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

Sidebar

Related Questions

Possible Duplicate: Can php variable hold harmfull code safely? Let's say I have array
Is it possible to create an environment to safely run arbitrary Python scripts under
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Is is possible to memcpy from a double array to a float array safely?
Can I safely rename the cygdrive folder? Also, I would like to add other
Possible Duplicate: Python, opposite function urllib.urlencode I have a url string which is already
I feel like this question has been answered but no implementation I've found from
I would like to be able to spawn a process in python and have

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.