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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:50:07+00:00 2026-06-17T23:50:07+00:00

In my code, I have a function that operates with static random engine generators

  • 0

In my code, I have a function that operates with static random engine generators and I would like to be able to throw an error if the user tries to call this function from threads different from the main one.

As an example, if you consider the following function :

void f()
{
    if (/* SOMETHING */) {
        throw std::future_error("ERROR = f() : cannot be executed in parallel");
    }
}

what would be /* SOMETHING */ ?

  • 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-17T23:50:08+00:00Added an answer on June 17, 2026 at 11:50 pm

    A much, much, much better solution would be to make your retrieval of random numbers thread-safe.

    This can be done one of two ways:

    1. Acquire a mutex on retrieval of random numbers.
    2. Give any threads that need to generate random numbers their own random object.

    Lets talk about #1. Since you propose wrapping your calls to get random numbers, it isn’t too farfetched to wrap those calls with a mutex. Rather than simply presenting the problem to the user at runtime (which is generally a really poor development choice), we’ve just solved the problem instead. The only downside is that we’ve slowed down calls to our random number generator — which is probably acceptable, but if it isn’t…onto option #2.

    And, for #2… You say that this is a static random number generator. Perhaps that means that you cannot make another copy of it, but this is worth mentioning anyway. rand and srand are like this. But, if you are using something like C++11’s std::uniform_int_distribution, you should have no problem creating a copy for each thread. (Perhaps consider std::thread_local if you cannot find a good place to put each thread’s copy.)

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

Sidebar

Related Questions

I'd like to write a function that would have some optional code to be
Assume that I have a MemoryStream and function that operates on bytes. Current code
In my Java code I have function that gets file from the client in
I have a function that sending a code gets a description through various formatings,
I have a function that finds a regex thingy, then replaces with php code.
I need help with my C code. I have a function that sets a
I have this code snippet inside a function that checks if an object exists
I have written code to perform a function that could take a while to
I have the following piece of code from a function that takes the host
this is function.php of a wordpress theme that NOD32 says this code have a

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.