首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在包装类scoped_thread中运行的线程中缺少一个输出

在包装类scoped_thread中运行的线程中缺少一个输出
EN

Stack Overflow用户
提问于 2018-03-30 12:47:06
回答 1查看 47关注 0票数 0

如果要使用www.ideone.com运行此程序

代码语言:javascript
复制
#include <iostream>
#include <thread>
#include <utility>
#include <stdexcept>

class scoped_thread
{
private:
    std::thread t;

public:
    explicit scoped_thread( std::thread t ) : t( std::move( t ) )
    {
        if ( not this->t.joinable() )
        {
            throw std::logic_error( "No thread" );
        }
    }

    ~scoped_thread()
    {
        t.join();
    }

    scoped_thread( const scoped_thread & ) = delete;
    scoped_thread & operator =( const scoped_thread & ) = delete;
};

void h()
{
    std::cout << "h() is running\n";
    for ( size_t i = 0; i < 10000; i++ );
    std::cout << "exiting h()\n";
}

void f()
{
    scoped_thread t( std::thread( h ) );
}

int main() 
{
    f();

    std::thread t( h );
    t.join();

    return 0;
}

那么输出是

代码语言:javascript
复制
h() is running
exiting h()

它对应于main中启动的线程t

但是,没有来自使用类scoped_thread启动的线程的simialr输出。为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-30 12:58:44

最令人烦恼的Parse

代码语言:javascript
复制
scoped_thread t( std::thread( h ) );

这定义了一个函数t,它使用一个名为hstd::thread并返回一个scopted_thread。要实际声明对象,请声明:

代码语言:javascript
复制
scoped_thread t{ std::thread(h) };
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49574748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档