Hello,i am writing an application which involves invoking multiple threads to pick data from a queue and then delete or pop the retrieved data.My codes work well for this purpose but then my application suddenly crashes with a dialog with the following error message:
Debug assertion failed!
Program:....
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
------------------------------------------------------------------------------
My codes:
#include <string>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/bind.hpp>
#include <ctime>
#include <cstdlib>
#include <boost/ref.hpp>
#include <boost/thread/mutex.hpp>
/*
* This class is responsible for the creation of multi-thread parses for simultaneous parsing processes.
*/
using namespace std;
class Parser
{
private:
queue<string>* Pages_Queue; //Holds the a pointer to the unparsed pages queue
int nParser; //Holds the number of parsers to work with
int PID; //The parser id
boost::thread_group threads;
boost::mutex mutex_;
public:
//Constructor
Parser(int pid,int nparser=10)
{
this->nParser=nparser;
this->PID=pid;
}
//Destructor
~Parser(){};
/*
*Loads the unparsed pages queue
*/
bool LoadPages(queue<string>* p_queue)
{
Pages_Queue=p_queue;
return true;
}
/*
* Returns the number of pages in the queue
*/
int GetQueueSize() const
{
return Pages_Queue->size();
}
/*
* Sets the number of parses to work with
*/
bool SetNumParser(int num)
{
this->nParser=num;
return true;
}
/*
* Returns the number of parsers
*/
int GetNumParser()
{
return this->nParser;
}
void Start(int id)
{
while(true)
{
try
{
if(Pages_Queue->size() > 0)
{
//boost::posix_time::seconds workTime(5);
//boost::this_thread::sleep(workTime);
//boost::mutex::scoped_lock lock(mutex_);
string page=Pages_Queue->front();
cout << "THREAD: " << id << endl << "Queue Size: " << Pages_Queue->size() << endl << page << "\n\n\n\n\n\n\n";
//Pages_Queue->pop();
}
else
{
cout << "\n\n FINISHED";
break;
}
}
catch(...)
{
cout << "\n\n\nUnknown error";
}
}
}
/*
* Creates a new child parser to act on queue.
*/
bool Parse(int id)
{
//Creating pointer to Start function
//typedef void(Parser::*Start_Prt)(int a);
//Start_Prt prt_s=&Parser::Start;
//Create the thread and pass the Start pointer to the thread
time_t now=time(0);
tm* localtm2 = localtime(&now);
cout << "Child parser created: " << id << " at " << asctime(localtm2) << endl;
//boost::thread pThread(boost::bind(boost::mem_fn(&Parser::Start),this,id));
threads.create_thread(boost::bind(boost::mem_fn(&Parser::Start),this,id));
return true;
}
/*
* Begins the parsing process
*/
bool Begin()
{
for(int i=0; i<this->nParser; i++)
{
this->Parse(i);
}
boost::thread::yield();
threads.join_all();
return true;
}
};
Its a little messy though.Please any help will be appreciated
Thanks