自定义线程安全STL分配器的想法? [英] Idea for custom thread-safe STL allocator?
问题描述
大家好,
我正在开发一些软件,它会创建一个信息树。对于
每个节点,目前我正在覆盖新运算符,因为它是一个
要求,在初始化之后,不会分配新的内存。
它还需要是线程安全的,并且每个线程都有一个上下文,所以任何
节点的分配目前看起来像这样:
new(context)Node(...);
我的分配器使用上下文来保证不同线程的内存安全。
I想要使用STL,并编写一个自定义分配器模板,用于传递给STL容器(具体地图和矢量)。
唯一的问题是,当我调用分配器时,我需要找出让STL使用
的方法。我想不出一个
的方法告诉自定义STL分配器在容器初始化之前在类中存储了一个自定义参数
。
任何想法?
Brian
Hi all,
I am developing some software, that creates a tree of information. For
each node, currently I am overriding the new operator, because it is a
requirement that after initialization, no new memory may be allocated.
It also needs to be thread safe, and each thread has a context, so any
allocation of nodes currently looks like this:
new (context) Node(...);
My allocator uses the context to keep memory from different threads safe.
I want to use STL, and write a custom allocator template for passing to
the STL containers (map and vector specifically).
The only problem, is that I need to figure out a way to have the STL use
the context as well, when it calls the allocator. I cant figure out a
way to tell the custom STL allocator to have a custom parameter stored
in the class before container initialization.
Any ideas?
Brian
推荐答案
2004年1月12日星期一12:25: 22 -0500,Brian Genisio
< Br ********** @ yahoo.com>写道:
On Mon, 12 Jan 2004 12:25:22 -0500, Brian Genisio
<Br**********@yahoo.com> wrote:
大家好,
我正在开发一些软件,它创建了一个信息树。对于每个节点,目前我正在覆盖新的运算符,因为它是一个要求,在初始化之后,不会分配新的内存。
它也需要是线程安全,每个线程都有一个上下文,所以任何
节点的分配目前看起来像这样:
new(context)节点(...);
我的分配器使用上下文来保证来自不同线程的内存安全。
我想使用STL,并编写一个自定义分配器模板,用于传递给STL容器(具体是map和vector)。
唯一的问题是,当我调用分配器时,我需要找出让STL使用上下文的方法。我无法找到一种方法告诉自定义STL分配器在容器初始化之前在类中存储自定义参数。
任何想法?
Hi all,
I am developing some software, that creates a tree of information. For
each node, currently I am overriding the new operator, because it is a
requirement that after initialization, no new memory may be allocated.
It also needs to be thread safe, and each thread has a context, so any
allocation of nodes currently looks like this:
new (context) Node(...);
My allocator uses the context to keep memory from different threads safe.
I want to use STL, and write a custom allocator template for passing to
the STL containers (map and vector specifically).
The only problem, is that I need to figure out a way to have the STL use
the context as well, when it calls the allocator. I cant figure out a
way to tell the custom STL allocator to have a custom parameter stored
in the class before container initialization.
Any ideas?
模板< class T>
类myallocator
{
上下文m_context;
public:
// allocator typedef
模板< class U>
struct rebind
{
typedef myallocator< U>其他;
};
myallocator(上下文const& context)
:m_context(context)
{
}
模板< class U>
myallocator(myallocator< U> const& other)
:m_context(other.m_context)
{
}
模板< class U>
myallocator& operator =(myallocator< U> const& other)
{
m_context = other.m_context;
return * this;
}
T * allocate(size_type n,void * hint = 0)
{
return :: operator new(n * sizeof(T),m_context);
}
//等。
};
Tom
C ++ FAQ: http://www.parashift.com/c++-faq-lite/
C常见问题: http://www.eskimo.com/~scs/C-faq/top.html
tom_usenet写道:
tom_usenet wrote:
2004年1月12日星期一12:25:22 -0500,Brian Genisio >< Br ********** @ yahoo.com>写道:
On Mon, 12 Jan 2004 12:25:22 -0500, Brian Genisio
<Br**********@yahoo.com> wrote:
大家好,
我正在开发一些软件,它创建了一个信息树。对于每个节点,目前我正在覆盖新的运算符,因为它是一个要求,在初始化之后,不会分配新的内存。
它也需要是线程安全,每个线程都有一个上下文,所以任何
节点的分配目前看起来像这样:
new(context)节点(...);
我的分配器使用上下文来保证来自不同线程的内存安全。
我想使用STL,并编写一个自定义分配器模板,用于传递给STL容器(具体是map和vector)。
唯一的问题是,当我调用分配器时,我需要找出让STL使用上下文的方法。我无法找到一种方法告诉自定义STL分配器在容器初始化之前在类中存储自定义参数。
任何想法?
Hi all,
I am developing some software, that creates a tree of information. For
each node, currently I am overriding the new operator, because it is a
requirement that after initialization, no new memory may be allocated.
It also needs to be thread safe, and each thread has a context, so any
allocation of nodes currently looks like this:
new (context) Node(...);
My allocator uses the context to keep memory from different threads safe.
I want to use STL, and write a custom allocator template for passing to
the STL containers (map and vector specifically).
The only problem, is that I need to figure out a way to have the STL use
the context as well, when it calls the allocator. I cant figure out a
way to tell the custom STL allocator to have a custom parameter stored
in the class before container initialization.
Any ideas?
模板< class T>
类myallocator
{context m_context;
public:
// allocator typedefs
template< class U>
struct rebind
{
typedef myallocator< U>其他;
};
myallocator(Context const& context)
:m_context(context)
{
}
模板< class U>
myallocator(myallocator< U> const& other)
:m_context(other.m_context)
{
}
模板< class U>
myallocator& operator =(myallocator< U> const& other)
{
m_context = other.m_context;
return * this;
}
T * allocate (size_type n,void * hint = 0)
{
return :: operator new(n * sizeof(T),m_context);
}
// //等等
};
Tom
C ++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
感谢您的信息...我非常有用。我唯一的问题:怎么
m_context是否设置好了?
假设我使用了一个整体列表:
std :: list< int,myallocator< int>> myList;
如何将上下文信息输入到容器'
分配器的副本中?
非常感谢,
Brian
Thanks for the info... I is very useful. The only question I have: How
does m_context ever get set?
Lets say I use a list of ints:
std::list<int, myallocator<int>> myList;
How do I get the context information in to the container''s copy of the
allocator?
Thanks a lot,
Brian
Brian Genisio写道:
Brian Genisio wrote:
tom_usenet写道:
tom_usenet wrote:
2004年1月12日星期一12:25:22 -0500,Brian Genisio
< Br ********** @ yahoo.com>写道:
On Mon, 12 Jan 2004 12:25:22 -0500, Brian Genisio
<Br**********@yahoo.com> wrote:
大家好,
我正在开发一些软件,它创建了一个信息树。
对于每个节点,目前我正在覆盖新的运算符,因为它要求在初始化之后,不能分配新的内存。
它还需要是线程安全的,并且每个thread有一个上下文,所以当前任何节点分配都是这样的:
new(context)Node(...);
我的分配器使用保持内存不同线程的上下文
安全。
我想使用STL,并编写一个自定义分配器模板,用于将
传递给STL容器(具体地说是map和vector)。
唯一的问题是,当我调用分配器时,我需要找出让STL使用上下文的方法。我无法想出一种方法告诉自定义STL分配器在容器初始化之前有一个自定义参数存储在类中。
任何想法?
template< class T>
class myallocator
{context m_context;
public:
// allocator typedef
template< ;类U>
结构重新绑定
{
typedef myallocator< U>其他;
};
myallocator(Context const& context):m_context(context)
{
}
模板<类U>
myallocator(myallocator< U> const& other)
:m_context(other.m_context)
{
}
模板<类U> ;
myallocator& operator =(myallocator< U> const& other)
{
m_context = other.m_context;
return * this;
}
T * allocate (size_type n,void * hint = 0)
{return :: operator new(n * sizeof(T),m_context);
}
//etc.
C ++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
感谢您的信息...我非常有用。我唯一的问题是:m_context是如何设置的?
让我说我使用了一个整体列表:
std :: list< int,myallocator< ; INT>> myList;
Thanks for the info... I is very useful. The only question I have: How
does m_context ever get set?
Lets say I use a list of ints:
std::list<int, myallocator<int>> myList;
小心。
std :: list< int,myallocator< int> > myList;
如何将上下文信息输入到容器的
分配器副本中?
非常感谢,
Brian
Careful.
std::list<int, myallocator<int> > myList;
How do I get the context information in to the container''s copy of the
allocator?
Thanks a lot,
Brian
这篇关于自定义线程安全STL分配器的想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!