Code Monkey home page Code Monkey logo

Comments (1)

ybainier avatar ybainier commented on July 17, 2024

Hey,
why don't you create a nested container per thread? Everything you resolve in the nested container will have the lifetime of the thread. It is already available and pretty simple.

This will give (maybe again sorry) you a look at the resolution mechanism:

class ThreadLocal
{
public:
    ThreadLocal(shared_ptr< Container >& container,
                shared_ptr< TransientDependency > transientDependency,
                shared_ptr< SingleInstanceDependency > singleInstanceDependency)
    {
        ContainerBuilder builder;
        builder.registerType< UnknownSingleFromTopLevel >().singleInstance();
        
        auto nestedContainer = builder.buildNestedContainerFrom(*container);
        m_worker = nestedContainer->resolve< Worker >();
    }
};

class Worker
{
public:
    Worker(shared_ptr< TransientDependency > transientDep,
           shared_ptr< SingleInstanceDependency > singleInstanceDep,
           shared_ptr< NotThreadLocalSingleInstanceDep > notThreadLocalSingleInstanceDep,
           shared_ptr< UnknownToTopLevelSingleInstance > unknownToTopLevelSingleInstance);
};

now the configuration:

ContainerBuilder builder;
builder.registerType< SingleInstanceDependency >().singleInstance();
builder.registerType< NotThreadLocalSingleInstanceDep >().singleInstance();

auto container = builder.build();

for (auto i = 0; i < 8; ++i)
{
   thread([=]() { container->resolve< ThreadLocal >(); });
}
  • SingleInstanceDependency and NotThreadLocalSingleInstanceDep will always be shared accross all threads because they are configured inside the toplevel container; that's true even if it is resolved by the nested container.
  • Worker and TransientDependency are transient by default, that is, a fresh instance is created everytime they are resolved.
  • UnknownToTopLevelSingleInstance cannot be known from the top level container. This could be the trick you need.

So I guess you could split the configuration like so:

  • extend ContainerBuilder for the components of the whole app
  • extend ContainerBuilder for the components that are supposed to be thread local. That way, everytime you instantiate a thread, you can create a nested container with these merged capabilities.

Am I missing something or does this achieves your goal?

from hypodermic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.