c++ - boost::threads 示例和堆损坏消息

标签 c++ multithreading boost-thread heap-corruption threadgroup

我是 boost::threads 的新手,我阅读了文档,但在实践中应用它时遇到了一些问题,也许你可以帮忙?首先,我花时间编写了一个自包含的代码 list ,它演示了我尚不能理解的 2 种行为......

该程序允许用户发出 3 个不同的命令,

  • 任务[名称]
  • 信息
  • 退出

目的是 task 将在新线程上启动一些工作,但在执行工作时返回到命令提示符。用户可以发出 info 命令来找出哪些任务已经完成,哪些还没有。

我使用的是双核 Win7 机器和 Visual Studio 2008 Express。

问题1>

发出命令,task p1 p2 p3,开始运行 3 个任务。这可以通过发出 info 来检查。几秒钟后工作完成,但由于某种原因,完成标志并不总是在 1 或 2 个任务上设置为真。

问题2>

然后退出程序会产生以下消息:

Windows has triggered a breakpoint in example.exe. This may be due to a corruption of the heap, which indicates a bug in example.exe or any of the DLLs it has loaded. This may also be due to the user pressing F12 while example.exe has focus. The output window may have more diagnostic information.

希望您能重现此行为并提供帮助。

提前致谢。 亚历克斯。

    //WARNING: THIS CODE DOES NOT BEHAVE EXACTLY AS INTENDED
#include <iostream>  
#include <string>
#include <sstream>
#include <boost/thread.hpp>  

using namespace std;

class task {
public:
    string mname;
    bool completed;
    void start()
    {
        int a = 0;
        for (int i=0 ; i<10000; i++)
        {
            for (int j=0 ; j<100000; j++)
            {
                a= i*2;
            }
        }
        this->completed = true;
    }
    task(string name)
    {
        mname = name;
        completed = false; 
    }
};

class taskManager{
    public:
        boost::thread_group threads;
        void startTask( string name )
        {
            //add new task to vector list           
            mtasks.push_back( task(name) );
            // execute start() on a new thread
            threads.create_thread( boost::bind( &task::start, &mtasks.back()) );
        }
        int tasksTotal()
        {
            return mtasks.size();
        }
        string taskInfo(int i)
        {
            string compstr("Not Completed");
            if ( mtasks[i].completed == true )
            {
                compstr = "Completed";
            }
            return mtasks[i].mname + " " + compstr; 
        }
    private:
        vector<task> mtasks; 
};

int main(int argc, char* argv[])  
{
    string cmd, temp;
    stringstream os;
    bool quit = false;
    taskManager mm;

    cout << "PROMPT>";

    while (quit == false)
    {
        //Wait for a valid command from user
        getline(cin,cmd);

        // Reset stringstream and assign new cmd string
        os.clear(); 
        os << "";
        os << cmd;
        //parse input string
        while (os >> temp) 
        {               
            if ( temp.compare("task") == 0 )
            {
                while (os >> temp) { mm.startTask( temp ); }                     
            }
            if ( temp.compare("info") == 0 )
            { 
                // Returns a list of all completed and not completed tasks
                for (int i = 0; i<mm.tasksTotal(); i++)
                {
                    cout << mm.taskInfo(i).c_str() << endl;
                }                           
            }
            if ( temp.compare("quit") == 0 ){ quit = true; }
        }

        cout << "PROMPT>";
    }

    mm.threads.join_all();      

    return 0;  
};

最佳答案

taskManager::startTask 方法中的代码有问题:

mtasks.push_back( task(name) );
// execute start() on a new thread
threads.create_thread( boost::bind( &task::start, &mtasks.back())

这里的问题是,在推回一个新任务时,您的 vector 可能必须重新分配一些空间,这样会使对旧 vector 元素的引用无效,例如以下对 taskinfo 的调用将引用错误的元素。当您删除旧元素时,您的堆会以某种方式损坏。

一个简单的解决方法是在 taskManager 类的构造函数中为 vector 保留一些空间,但是您可能应该改为更改任务/taskmanager 模型的设计。另一种方法是使用 std::deque,因为它不会重新分配内存。

关于c++ - boost::threads 示例和堆损坏消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10373310/

相关文章:

c# - 当我调用 BeginXXX() 时,CLR 何时创建 IO 线程

python - 在 Python 中使用多线程 blas 实现和多处理是否值得?

c++ - 同时从 2 个线程读取全局变量

c++ - 尝试将线程创建方法从 C 迁移到 C++ 不起作用

c++ - 关于我在 OS X 上可用的各种 C++ 编译器的许多问题

c++ - 找到正确的设计方法

c++ - 命名类时使用命名空间或前缀供应商名称?

c++ - 为什么 map<bool,int> m={{1,2},{3,4},{5,0}};尺寸 1 而不是 3?

c++ - 如果有足够多的事件,boost::mutex 可以锁定操作系统吗?

c++ - 不间断地构建 Boost.Thread