C ++ 11 std :: thread奇怪的行为 [英] C++ 11 std::thread strange behavior

查看:105
本文介绍了C ++ 11 std :: thread奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用std :: thread和C ++ 11进行一些实验,并且遇到了奇怪的行为. 请看下面的代码:

I am experimenting a bit with std::thread and C++11, and I am encountering strange behaviour. Please have a look at the following code:

#include <cstdlib>
#include <thread>
#include <vector>
#include <iostream>

void thread_sum_up(const size_t n, size_t& count) {
  size_t i;
  for (i = 0; i < n; ++i);
  count = i;
}

class A {
public:
  A(const size_t x) : x_(x) {}

  size_t sum_up(const size_t num_threads) const {
    size_t i;
    std::vector<std::thread> threads;
    std::vector<size_t> data_vector;
    for (i = 0; i < num_threads; ++i) {
      data_vector.push_back(0);
      threads.push_back(std::thread(thread_sum_up, x_, std::ref(data_vector[i])));
    }

    std::cout << "Threads started ...\n"; 

    for (i = 0; i < num_threads; ++i)
      threads[i].join();

    size_t sum = 0;
    for (i = 0; i < num_threads; ++i)
      sum += data_vector[i];
    return sum;
  }

private:
  const size_t x_;
};

int main(int argc, char* argv[]) {
  const size_t x = atoi(argv[1]);
  const size_t num_threads = atoi(argv[2]);
  A a(x);
  std::cout << a.sum_up(num_threads) << std::endl;
  return 0;
}

这里的主要思想是我想指定多个执行独立计算的线程(在这种情况下,是简单的增量). 在所有线程完成之后,应该合并结果以便获得总体结果.

The main idea here is that I want to specify a number of threads which do independent computations (in this case, simple increments). After all threads are finished, the results should be merged in order to obtain an overall result.

请澄清一下:这仅出于测试目的,目的是让我了解 C ++ 11线程可以工作.

Just to clarify: This is only for testing purposes, in order to get me understand how C++11 threads work.

但是,使用命令编译此代码时

However, when compiling this code using the command

g++ -o threads threads.cpp -pthread -O0 -std=c++0x

在Ubuntu盒子上,当我执行生成的二进制文件时,我会得到非常奇怪的行为. 例如:

on a Ubuntu box, I get very strange behaviour, when I execute the resulting binary. For example:

$ ./threads 1000 4
Threads started ...
Segmentation fault (core dumped)

(应产生的输出为4000)

(should yield the output: 4000)

$ ./threads 100000 4
Threads started ...
200000

(应产生输出:400000)

(should yield the output: 400000)

有人知道这里发生了什么吗?

Does anybody has an idea what is going on here?

提前谢谢!

推荐答案

您的代码有很多问题(关于2-3个错误,甚至请参见thread_sum_up),但是通过浏览代码发现的主要错误是在这里:

Your code has many problems (see even thread_sum_up for about 2-3 bugs) but the main bug I found by glancing your code is here:

data_vector.push_back(0);
threads.push_back(std::thread(thread_sum_up, x_, std::ref(data_vector[i])));

看,当您push_back进入向量时(我说的是data_vector),它可以在内存中移动所有先前的数据.但是随后您获取线程的单元格的地址(引用),然后再次推回(使先前的引用无效)

See, when you push_back into a vector (I'm talking about data_vector), it can move all previous data around in memory. But then you take the address of (reference to) a cell for your thread, and then push back again (making the previous reference invalid)

这将导致您崩溃.

为简便起见,请在创建后立即添加data_vector.reserve(num_threads);.

For an easy fix - add data_vector.reserve(num_threads); just after creating it.

编辑(根据您的要求)-thread_sum_up

Edit at your request - some bugs in thread_sum_up

void thread_sum_up(const size_t n, size_t& count) {
  size_t i;
  for (i = 0; i < n; ++i); // see that last ';' there? means this loop is empty. it shouldn't be there
  count = i; // You're just setting count to be i. why do that in a loop? Did you mean +=?
}

这篇关于C ++ 11 std :: thread奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆