C++线程错误:未分配要释放的指针

C++ Threading error: pointer being freed was not allocated

本文关键字:释放 指针 分配 线程 错误 C++      更新时间:2023-10-16

我正在学习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;
}