C++线程错误:未分配要释放的指针
C++ Threading error: pointer being freed was not allocated
我正在学习C++中的线程,并进行了一些测试,结果出现了这个错误。
这是我的代码:
#include <iostream>
#include <thread>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
static vector<string> thisVector;
void thread1() {
for (int i = 0; i < 400; i++){
thisVector.push_back(to_string(i));
}
cout << "Finished 1" << endl;
return;
}
void thread2() {
for (int i = 0; i < 400; i++){
thisVector.push_back(to_string(i));
}
cout << "Finished 2" << endl;
return;
}
int main(int argc, const char * argv[]) {
thread first(thread1);
thread second(thread2);
first.join();
second.join();
cout << "done threading" << endl;
cout << thisVector.size() << endl;
return 0;
}
奇怪的是,有时我会得到正确的输出,所以是800。有时我会得到一个比这个数字略低的数字,因为我不知道是什么原因???有时我会得到以下错误:
对象0x100400028的malloc:***错误:释放的指针未分配
如有任何帮助,我们将不胜感激!
感谢
std::vector
不是线程安全的。您有两个执行线程同时修改同一个std::vector
。在这种情况下,std::vector
上的操作应该受到互斥锁的保护。
其结果是未定义的行为。正如您所观察到的,有时它可能会工作,有时会产生错误的结果,但程序成功完成,有时程序崩溃。这就是"未定义行为"的含义。
值得一读的是C++编程语言,它涵盖了感兴趣的主题,如:线程、任务,但也在更高的概念级别future/promise、package_task、async。
在您的案例中,您不仅仅处理并发性。您必须处理并发线程的共享数据。你对它编程的方式并不能保证执行的顺序。这就是为什么你会得到那些奇怪的结果。
您提到的错误消息很可能是因为对共享向量的并发访问,它指出了问题所在:指针正在被释放(删除),但事先没有分配。你看不到指针,它是向量的内部成员。
您的解决方案是使用互斥锁来确保每当数据被推回向量时,它不会被另一个并发线程中断并适当地完成,这样下一个push_back只有在上一个线程完成时才开始。
您可以为互斥对象使用全局变量。然后,您需要适当地处理锁定/解锁,即,您必须说明何时获取互斥体(在push_back之前)和何时释放它(在push_back之后)。由于您只使用一个互斥锁,所以这应该相当简单并且应该可以工作。
您可以尝试以下操作:
#include <iostream>
#include <thread>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
static vector<string> thisVector;
//Global MUTEX
mutex mv;
//
void thread1() {
for (int i = 0; i < 400; i++){
//Lock before and unlock after push back
mv.lock();
thisVector.push_back(to_string(i));
mv.unlock();
}
cout << "Finished 1" << endl;
return;
}
void thread2() {
for (int i = 0; i < 400; i++){
mv.lock();
thisVector.push_back(to_string(i));
mv.unlock();
}
cout << "Finished 2" << endl;
return;
}
int main(int argc, const char * argv[]) {
thread first(thread1);
thread second(thread2);
first.join();
second.join();
cout << "done threading" << endl;
cout << thisVector.size() << endl;
return 0;
}
- 正在理解智能指针,但出现错误:未分配正在释放的指针
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- 如何在向量中释放指针?
- 在C++中释放内存期间,迭代器与指针有何不同
- 包含矢量指针的结构的内存释放问题
- 错误:malloc:对象 0x7f9edf504080 的 *** 错误:未分配正在释放的指针
- 错误:释放指针未分配在矢量模板类C 中
- 如何在指针初始化为数组的第一个索引后释放指针
- 如何释放指针树中的所有内存
- 从 std::align 中释放指针
- C :释放指针没有分配在课堂上
- 构造向量 C++ 后无法释放指针
- nullptr是否释放指针的内存
- C/C++ 通过调用引用或直接释放指针
- 分段释放指针向量的错误
- 有效地从指向对象本身释放指针
- 如何释放指针向量中的元素
- Sqlite c/c++api,负责从sqlite3_column_text中释放指针
- 释放指针后分配给指针
- 正在释放指针