在类中使用std :: thread时出错

时间:2016-08-18 11:00:06

标签: multithreading c++11

我尝试在Visual Studio 2013中使用std :: thread。它可以正常工作。

function update() {
    var pointGroup = nodeGroup.selectAll('.node')
        .data(data.nodes, function(d) { return d ? d.id : this.id; });

    pointGroup.exit().remove();

    var newNodeGroups = pointGroup.enter().append('g')
          .attr('class', 'node');

    newNodeGroups.append('circle')
          .attr('r', function(d) { return circleRadius[d.type] });

    newNodeGroups.append('text')
          .text(function(d) { return d.id; })
          .attr('class', function(d) { return d.type; });

    var allLinks = linkGroup.selectAll('line')
        .data(data.links, function(d) { return d.source+'-'+d.target; });

    allLinks.exit().remove();

    newLinks = allLinks.enter().append('line')
        .attr('class', 'link');

    var circles = nodeGroup.selectAll('circle');
    var text = nodeGroup.selectAll('text');
    var lines = linkGroup.selectAll('line');

    function ticked() {
        lines
            .attr("x1", function(d) { return d.source.x; })
            .attr("y1", function(d) { return d.source.y; })
            .attr("x2", function(d) { return d.target.x; })
            .attr("y2", function(d) { return d.target.y; });

        circles
            .attr("cx", function(d) { return d.x; })
            .attr("cy", function(d) { return d.y; });

        text
            .attr("x", function(d) { return d.x; })
            .attr("y", function(d) { return d.y; });
    }

    simulation
        .on('tick', ticked)
        .restart();
};

但是当我尝试在类中创建线程时,我遇到了一些编译错误。

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono>    // std::chrono::seconds
#include <iostream>  // std::cout
#include <thread>    // std::thread, std::this_thread::sleep_for
#include <vector>
void thread_task(int n) {
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "hello thread "
        << std::this_thread::get_id()
        << " paused " << n << " seconds" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<std::thread> threads;
    std::cout << "Spawning 5 threads...\n";
    for (int i = 0; i < 5; i++) {
        threads.emplace_back( std::thread(thread_task, i + 1));
    }
    std::cout << "Done spawning threads! Now wait for them to join\n";
    for (auto& t : threads) {
        t.join();
    }
    std::cout << "All threads joined.\n";
    return 0;
}

错误消息将在下面复制。

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono>    // std::chrono::seconds
#include <iostream>  // std::cout
#include <thread>    // std::thread, std::this_thread::sleep_for
#include <vector>
class MyClass
{
public:
    MyClass();
    void thread_task(int n);
    ~MyClass();
private:
};
MyClass::MyClass()
{
    std::vector<std::thread> threads;
    std::cout << "Spawning 5 threads...\n";
    for (int i = 0; i < 5; i++) {
        threads.emplace_back(std::thread(&MyClass::thread_task, i + 1));
    }
    std::cout << "Done spawning threads! Now wait for them to join\n";
    for (auto& t : threads) {
        t.join();
    }
    std::cout << "All threads joined.\n";
}
void MyClass::thread_task(int n) {
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "hello thread "
        << std::this_thread::get_id()
        << " paused " << n << " seconds" << std::endl;
}
MyClass::~MyClass(){}
int _tmain(int argc, _TCHAR* argv[])
{   
    MyClass test();
    return 0;
}

导致问题的原因是什么?

感谢@malchemist,当添加这个解决问题时。但是这让我感到困惑,这段代码code error仍然有一些编译错误,甚至写得像答案所示。

任何人都可以帮助我吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

你必须在新线程中指定一个调用函数的对象。试试这个:

threads.emplace_back(std::thread(&MyClass::thread_task, this , i + 1));