Hi, thanks for all the help, everything is working perfect now.
I have another question, I need to access the shared memory segment from
several process and all at the same time. I tried to use pthreads but that
didn't work.
I was looking at boost::interprocess threads but I'm not sure how to
implement the mutex.
this is the code I have:
#include
#include
#include
#include
#include
#include <cstdlib>
#include <string>
#include <iostream>
#include <map>
#include <sstream>
using namespace boost::interprocess;
typedef managed_shared_memory::segment_manager SegmentManager;
typedef allocator VoidAllocator;
typedef allocator CharAllocator;
typedef basic_string
BasicString;
class Action
{
int id;
BasicString task;
public:
Action(int num, const char *name, const VoidAllocator &void_alloc)
:id(num), task(name,void_alloc)
{}
void setId(int num);
void setTask(BasicString newTask);
int getId();
};
int Action::getId() {
return id;
}
void Action::setId(int num) {
id = num;
}
void Action::setTask(BasicString newTask) {
task = newTask;
}
typedef allocator ActionAllocator;
typedef std::pair MapValueType;
typedef std::pair MovableToMapValueType;
typedef allocator MapValueTypeAllocator;
typedef map MyMap;
int main(int argc, char** argv) {
//Remove shared memory on construction and destruction
struct shm_remove {
shm_remove() {shared_memory_object::remove("MySharedMemory"); }
~shm_remove() {shared_memory_object::remove("MySharedMemory"); }
}remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
VoidAllocator alloc_inst(segment.get_segment_manager());
MyMap *myMap = segment.construct<MyMap>("Map"
)(std::less<BasicString>(),alloc_inst);
std::string keyword;
std::string helper;
int i=0;
for(int adder = 0; adder<=1000; adder++)
{
keyword = "Hello";
std::stringstream out;
out << adder;
helper = out.str();
keyword = keyword + helper;//Creates a string "Hello" + adder
BasicString key(keyword.c_str(), alloc_inst);//string to BasicString
Action action(i,"Hello", alloc_inst);
MapValueType value(key, action);
myMap->insert(value);
i++;
}
How do I have to set the mutex??
Thanks!
Dann
On Wed, Feb 3, 2010 at 11:24 AM, Steven Watanabe wrote:
AMDG
Daniel Veneros wrote:
Thanks,
I fixed some things and its running now.
What I don't know how to do is to declarate/allocate a custom class..
I got this:
typedef managed_shared_memory::segment_manager SegmentManager;
typedef allocator CharAllocator;
typedef basic_string
BasicString;
typedef allocator IntAllocator;
class Action
{
int id;
BasicString task;
public:
Action(int num, const char *name, const VoidAllocator &void_alloc)
:id(num), task(name,void_alloc)
{}
void setId(int num);
void setTask(BasicString newTask);
};
void Action::setId(int num) {
id = num;
}
void Action::setTask(BasicString newTask) {
task = newTask;
}
Now... how should I allocate the "Class" object??
Any Idea? Thanks!!
I assume that by Class you mean the Action class?
If this class is used at the top level, you can use
managed_shared_memory::construct. If you want
to use an allocator with it, the commands are
allocator alloc(...);
// allocate memory
Action* action = alloc.allocate(1);
// construct an object using placement new
new (action) Action(12, "action", ...);
In Christ,
Steven Watanabe
P.S. Please send messages like this to boost-users rather than directly to
me.