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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:44:58+00:00 2026-05-27T16:44:58+00:00

I have a class representing different kind of tools (GCC, LEX, YACC, …). Each

  • 0

I have a class representing different kind of tools (GCC, LEX, YACC, …).
Each instance is given a type representing the tool, and permit special configuration.

To handle default configuration, I have a set of maps and vector storing defaults values. As I want that class to be usable in every context, it would have to be thread-safe, thus to avoid race I implemented the following:

int Tool::addType(std::string typeName,
        std::string typeFlagName)
    throw (GP::Exceptions::LockAcquisitionException)
{   
    static std::timed_mutex mutex;
    std::unique_lock<std::timed_mutex> lock{mutex};

    int typeId = 0;

    if (lock.try_lock_for(std::chrono::microseconds(100)))
    {   
        int typeId = typeNames.size();
        typeNames[typeId] = typeName;
        typeFlagNames[typeId] = typeFlagName;
    }   
    else
    {   
        throw GP::Exceptions::LockAcquisitionException{"Unable to generate new type ID within 100 microseconds."};
    }   
    return typeId;
}

I would like to know if this is a good solution or if I miss something.
If this is okay, is there another solution less verbose?

  • 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-27T16:44:58+00:00Added an answer on May 27, 2026 at 4:44 pm

    Why not something like?

    class tool
    {
        std::atomic<int> index_;
        std::array<std::pair<std::string, std::string>, 2048> types_; // Should be more than enough room.
    
        int addType(std::string typeName, std::string typeFlagName)
        {   
            int id = index++;
    
            if(id >= types_.size())
                throw GP::Exceptions{"To many types."};
    
            types_[id] = std::make_pair(typeName, typeFlagName);
    
            return id;
        }
    
    };
    

    You could make this a bit smarter by using std::vector and have a lock only when it needs to be reallocated for a larger size.

    NOTE: the throw() clause is deprecated in C++11.

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

Sidebar

Related Questions

I have a class hierarchy representing different language constructs: Expression <- NumericLiteral UnaryExpression BinaryExpression
I have a Person class which has a String collection of aliases representing additional
I have a datastore model representing items in an ecommerce site: class Item(db.Model): CSIN
I have a Rails app representing a card game. There are several different types
Suppose for instance I'm defining a Complex class for representing complex numbers. I would
I have a class representing a finite-state machine, which should run in a forever
I have a JavaScript class representing a car, which is constructed using two parameters,
I have a class representing a user called Nick and I want to use
I have an array of PropertyInfo representing the properties in a class. Some of
I have class representing a unit of measure with a Decimal as the numeric.

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.