c++ - Getting core :: Program aborted with core -
i have used 2 classes: 1 dataprocessor processing file , count total number of instances of word in every line. tried multi-threading thread initiated take care of search in each line.
storing count each line in vector.
when executing without multi-threading, it's doing fine.
void dataprocessor::countinstance () { int pos = -1, count = 0; string linefromfile; cout << "in countinstance" <<endl; getline (m_file, linefromfile); <<<< not getting line read file while(true) { pos = linefromfile.find(mstr_word, ++pos); if (pos != std::string::npos) count++; else break; } mvec_countperline.push_back(count); cout <<mstr_word << "****" <<linefromfile <<endl; cout << mvec_countperline[0]<<endl; } dataprocessor::~dataprocessor() { m_file.close(); } //////// void *threadfunction (void *dp) { pthread_mutex_lock( &mutex1 ); dataprocessor *dprocessor = static_cast<dataprocessor *> (dp); <<<just curious if casting creating problem cout << "in threadfunction"<< endl; dprocessor->countinstance (); pthread_mutex_unlock( &mutex1 ); } class mythread { private: pthread_t threads[10]; public: mythread (); void createthread (dataprocessor *); ~mythread (); }; here creating threads in loop
void mythread::createthread (dataprocessor *dprocessor) { cout << "in create thread::"; int numlinesinfile, rc; numlinesinfile = dprocessor->getnumlinesinfile(); ( int =0; < numlinesinfile; i++) { rc = pthread_create(&threads[i], null, threadfunction, (void *)&dprocessor); if (rc) { cout << "error:unable create thread," << rc << endl; exit(-1); } } ( int =0; < numlinesinfile; i++) pthread_join(threads[i], null); pthread_exit(null); } mythread::~mythread () { } ######################################################## calling these 2 objects from:- dprocessor.openfile("example.txt"); dprocessor.setword(readdata); mythread thread; thread.createthread (&dprocessor); dprocessor.displayresult(); but getting:
in create thread::in threadfunction in countinstance terminate called after throwing instance of 'std::bad_cast' what(): std::bad_cast aborted (core dumped) can help me figure out reason this?
your cast says parameter pointer dataprocessor:
static_cast<dataprocessor *> (dp); but you're passing pointer pointer dataprocessor:
(void *)&dprocessor lose cast , ampersand:
rc = pthread_create(&threads[i], null, threadfunction, dprocessor); by way: never need cast pointer void*.
c++ core
No comments:
Post a Comment