简体   繁体   English

boost.thread-简单示例中的访问冲突

[英]boost.thread - access violation in a simple example

I'm just trying to run a very simple example of using threads with boost . 我只是试图运行一个非常简单的示例,将线程与boost Here is the code: 这是代码:

#include <iostream>
#include <boost/thread.hpp>

void workerFunc()
{
    std::cout << "hi" << std::endl;
}

int main(int argc, char* argv[])
{
    boost::thread workerThread(workerFunc);
    workerThread.join();
    return 0;
}

But I get an exception: 但我有一个例外:

(boost_thread-vc140-mt-1_59.dll) in test.exe: 0xC0000005: Access violation reading location 0x0000000000000019.

Despite the exception, the program works and I can see hi in console. 尽管有例外,该程序仍然可以运行,并且可以在控制台中看到hi I use VS2015, x64 configuration. 我使用VS2015,x64配置。 Where is my mistake? 我的错误在哪里?

There's clearly undefined behaviour, as there's nothing wrong with the code. 显然有未定义的行为,因为代码没有错。

So, what is wrong? 那么,什么错的?

There's likely an error in the build configuration, deployment (runtime environment), or a mismatch between those two. 构建配置,部署(运行时环境)中可能存在错误,或两者之间不匹配。

Specifically: 特别:

  • you might build your project with a compiler version/flags that make it binary incompatible with the libraries used. 您可能会使用一个编译器版本/标志来构建您的项目,该编译器版本/标志会使其二进制与所使用的库不兼容。
  • you might link to the import libraries of 1 build/version of boost and be finding a different version at runtime 您可能会链接到1个build / version of boost的导入库,并在运行时找到其他版本

Both conditions introduce Undefined Behaviour . 两种情况都会引入未定义的行为 Anything can happen. 什么都可能发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM