我试图从子进程中使用c ++中的同步队列。我在C ++()(http://www.internetmosquito.com/2011/04/making-thread-safe-queue-in-c-i.html)
中使用此同步队列我在boost中将队列修改为可序列化,并且还替换了使用的boost::mutex io_mutex_
来代替一个进程互斥(感谢@Sehe)boost::interprocess::interprocess_mutex io_mutex_
当锁定时
我将boost::mutex::scoped_lock lock(io_mutex_);
的所有行都更改为scoped_lock<interprocess_mutex> lock(io_mutex_);
template<class T>
class SynchronizedQueue
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & sQueue;
ar & io_mutex_;
ar & waitCondition;
}
... // queue implementation (see [http://www.internetmosquito.com/2011/04/making-thread-safe-queue-in-c-i.html][2])
}
在我的测试应用中,我创建了同步队列并在其中存储了100个此类实例:
class gps_position
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
public:
int degrees;
int minutes;
float seconds;
gps_position() {};
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}
};
消费者和生产者之间的共同定义:
char *SHARED_MEMORY_NAME = "MySharedMemory";
char *SHARED_QUEUE_NAME = "MyQueue";
typedef SynchronizedQueue<gps_position> MySynchronisedQueue;
生产者流程代码:
// Remove shared memory if it was created before
shared_memory_object::remove(SHARED_MEMORY_NAME);
// Create a new segment with given name and size
managed_shared_memory mysegment(create_only,SHARED_MEMORY_NAME, 65536);
MySynchronisedQueue *myQueue = mysegment.construct<MySynchronisedQueue>(SHARED_QUEUE_NAME)();
//Insert data in the queue
for(int i = 0; i < 100; ++i) {
gps_position position(i, 2, 3);
myQueue->push(position);
}
// Start 1 process (for testing for now)
STARTUPINFO info1={sizeof(info1)};
PROCESS_INFORMATION processInfo1;
ZeroMemory(&info1, sizeof(info1));
info1.cb = sizeof info1 ; //Only compulsory field
ZeroMemory(&processInfo1, sizeof(processInfo1));
// Launch child process
LPTSTR szCmdline = _tcsdup(TEXT("ClientTest.exe"));
CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, 0, NULL, NULL, &info1, &processInfo1);
// Wait a little bit ( 5 seconds) for the started client process to load
WaitForSingleObject(processInfo1.hProcess, 5000);
/* THIS TESTING CODE WORK HERE AT PARENT PROCESS BUT NOT IN CLIENT PROCESS
// Open the managed segment memory
managed_shared_memory openedSegment(open_only, SHARED_MEMORY_NAME);
//Find the synchronized queue using it's name
MySynchronisedQueue *openedQueue = openedSegment.find<MySynchronisedQueue>(SHARED_QUEUE_NAME).first;
gps_position position;
while (true) {
if (myQueue->pop(position)) {
std::cout << "Degrees= " << position.degrees << " Minutes= " << position.minutes << " Seconds= " << position.seconds;
std::cout << "\n";
}
else
break;
}*/
// Wait until the queue is empty: has been processed by client(s)
while(myQueue->sizeOfQueue() > 0) continue;
// Close process and thread handles.
CloseHandle( processInfo1.hThread );
我的消费者代码如下:
//Open the managed segment memory
managed_shared_memory segment(open_only, SHARED_MEMORY_NAME);
//Find the vector using it's name
MySynchronisedQueue *myQueue = segment.find<MySynchronisedQueue>(SHARED_QUEUE_NAME).first;
gps_position position;
// Pop each position until the queue become empty and output its values
while (true)
{
if (myQueue->pop(position)) { // CRASH HERE
std::cout << "Degrees= " << position.degrees << " Minutes= " << position.minutes << " Seconds= " << position.seconds;
std::cout << "\n";
}
else
break;
}
当我运行创建队列并创建子(消费者)进程的父进程(生产者)时,在尝试弹出&#39; pop&#39;从队列中。
我在这里做错了什么?任何的想法 ?感谢您的任何见解。这是我第一个使用boost和共享内存创建的应用程序。
我的目标是能够从多个进程中使用此队列。在上面的示例中,我只创建了一个子进程,以确保在创建其他子进程之前首先它可以正常工作。这个想法是队列将由项目提前填充,并且多个创建的过程将会弹出&#39; pop&#39;来自它的物品没有相互冲突。
答案 0 :(得分:3)
更新后的代码:
toString()
的“线程安全”函数的用途是什么?在处理返回值之前,它可能不再为空/只是空...这些被调用 竞争条件 并导致很难跟踪错误empty()
中使用了any
?由于队列的设计, typeid总是toString()
。gpsposition
(如果已经拥有字符串流,为什么还要进行字符串连接?)boost::lexical_cast<>
,empty()
,toString()
不是sizeOfQueue()
?我强烈建议您使用const
。这个似乎是你真正想要使用的东西(因为你是某种方式
这是一个修改后的版本,它将容器放在共享内存中并起作用:
boost::interprocess::message_queue