使用core中止程序
Getting core :: Program aborted with core
我使用了两个类:一个Dataprocessor,它将处理文件并计算每行中该单词的实例总数。尝试用多线程来做,线程将被启动来处理每行的搜索。
在vector中存储每行的计数。
在没有多线程的情况下执行,它做得很好。
void DataProcessor::countInstance ()
{
int pos = -1, count = 0;
string lineFromFile;
cout << "In countInstance" <<endl;
getline (m_file, lineFromFile); <<<< Not getting line read from 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 is creating a problem
cout << "IN ThreadFunction"<< endl;
dProcessor->countInstance ();
pthread_mutex_unlock( &mutex1 );
}
class MyThread
{
private:
pthread_t threads[10];
public:
MyThread ();
void createThread (DataProcessor *);
~MyThread ();
};
这里我在循环中创建线程
void MyThread::createThread (DataProcessor *dProcessor)
{
cout << "IN CREATE THREAD::";
int numLinesInFile, rc;
numLinesInFile = dProcessor->getNumLinesInFile();
for ( int i =0; i < numLinesInFile; i++)
{
rc = pthread_create(&threads[i], NULL,
ThreadFunction, (void *)&dProcessor);
if (rc)
{
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
for ( int i =0; i < numLinesInFile; i++)
pthread_join(threads[i], NULL);
pthread_exit(NULL);
}
MyThread::~MyThread ()
{
}
########################################################
Calling these two objects from:-
dProcessor.openFile("example.txt");
dProcessor.setWord(readData);
MyThread thread;
thread.createThread (&dProcessor);
dProcessor.displayResult();
但得到:
IN CREATE THREAD::IN ThreadFunction
In countInstance
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Aborted (core dumped)
有谁能帮我弄清楚这是为什么吗?您的强制转换表明该参数是指向DataProcessor
的指针:
static_cast<DataProcessor *> (dp);
但是你传递了一个指向指向DataProcessor
的指针的指针:
(void *)&dProcessor
去掉强制转换和&符号:
rc = pthread_create(&threads[i], NULL, ThreadFunction, dProcessor);
顺便说一下:您永远不需要将指针强制转换为void*
。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 是否有可能构建面向Linux和Windows的.Net Core C++ / CLI应用程序?
- 在.NET Core 3.1中运行托管C++/CLI程序集时,映像格式错误
- numpy.core.multiarray在通过Xcode中开发的C 应用程序调用Python模块时未能导入
- (GSTREAMER)如何使Taglib和Core Audio实用程序类一起工作
- 混合模式程序集(C++/CLI项目)在.NET Core上工作吗
- 为什么 C++ 程序运行结果为 "Segmentation fault (core dumped)"
- 使用core中止程序
- 购物清单程序:在 VIM "Segmentation fault (core dumped)" 中,但代码块工作