Comments (1)
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
andNotThreadLocalSingleInstanceDep
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
andTransientDependency
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)
- GUN C++ 4.7.3 Compiler error HOT 1
- Resolve other than shared_ptr (e.g. unique_ptr) HOT 5
- Current syntax for register factory with arguments HOT 5
- Pass parameters to register type HOT 1
- Resolve a chain of instantiate of explicit dependencies name?
- Compile errors: multiple definition of boost::*
- static assertion failed: TArg should be a complete type HOT 5
- Autowiring of factory methods HOT 2
- Question: register two interfaces for same concrete class HOT 2
- Fail to build in window boost1.7 gcc9
- Bug: interface is resolved twice from nested container if they are added as both .as<interface>() and .asSelf() HOT 2
- Bug: singleInstance not working as expected when resolving from nested containers HOT 2
- How to inject a factory for named instances? HOT 3
- Is it possible to specify the dependency explicitly while resolving HOT 4
- Issue when resolving same types in different shared libraries HOT 1
- ContainerBuilder doesn't have a virtual destructor HOT 1
- Hypodermic 2.5.3 won't link with Boost
- Is there a version without boost HOT 5
- Alternatives to `std::make_shared` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hypodermic.