std :: thread定义线程函数时的符号 [英] std::thread notation when defining the threaded function

查看:179
本文介绍了std :: thread定义线程函数时的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白 std :: thread rel =nofollow> here ,并转载如下:

I understand the std::thread notation presented here and reproduced as follows

#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>

void f1(int n)
{
    for (int i = 0; i < 5; ++i) {
    std::cout << "Thread " << n << " executing\n";
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

void f2(int& n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread 2 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

int main()
{
    int n = 0;
    std::thread t1; // t1 is not a thread
    std::thread t2(f1, n + 1); // pass by value
    std::thread t3(f2, std::ref(n)); // pass by reference
    std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
    t2.join();
    t4.join();
    std::cout << "Final value of n is " << n << '\n';
}

因为 f1 f2 内,但无法理解

because the definition of f1 and f2 is within main but fail to understand

#ifndef THREADED_H_
#define THREADED_H_

class Threadme
{
    long count;

public:

    Threadme();

    void run(void);

    void delay(long);
};

#endif

#include "threaded.h"
#include <iostream>
#include <chrono>

Threadme::Threadme() : count(0) {}

void Threadme::delay(long seconds)
{
    std::chrono::steady_clock::time_point end_t = std::chrono::system_clock::now() + std::chrono::seconds(seconds);

    while(std::chrono::system_clock::now() < end_t)
       ;
}

void Threadme::run(void)
{
    while(count < 10)
    {
        ++count;

        std::cout << count << std::endl;

        delay(1);
    }

}

#include <cstdlib>
#include <thread>

#include "threaded.h"

int main(int argc, char *argv[]){

    std::thread t1(&Threadme::run, Threadme());

    t1.join();

    return EXIT_SUCCESS;
}

特别是表达式 std :: thread t1 ; $ run 之外 main 。为什么引用& 以及为什么线程参数是一个构造函数调用?

specifically the expression std::thread t1(&Threadme::run, Threadme()); as it relates to defining the threaded function run outside of main. Why the reference & and why the thread parameters is a constructor invocation?

推荐答案

& Foo :: mem 其中 Foo 是一个类类型, Foo 的一个成员(函数或值)的 mem 是C ++表示法,成员(函数或值)。存在一个特殊的语法用于调用对象上的成员函数指针,但这通常通过使用 std :: mem_fun 来消除,它将成员函数指针转换为普通函数,其中第一个参数必须是成员函数取自的类型的对象。

&Foo::mem where Foo is a class type and mem a member (function or value) of Foo, is C++ notation for obtaining a pointer to a member (function or value). There exist a special syntax for invoking a member function pointer on an object, but this is usually sugared away by using std::mem_fun, which will turn a member function pointer into an ordinary function where the first argument has to be an object of the type the member function was taken from.

std :: thread 了解这里发生的情况,并且正确地执行:对作为第二个参数传递的对象调用 Foo :: mem

std::thread understands what is happening here and does exactly that: invoke Foo::mem on the object passed as the second argument.

一个小例子可以在本地重现这个实例,而不实际涉及 std :: thread

A small example to reproduce this locally without actually involving std::thread:

#include <functional>

class Foo { void mem() {} };

int main() {
  Foo f;
  f.mem(); // normal invoke
  auto func = std::mem_fun(&Foo::mem);

  func(std::ref(f)); // invoke mem on f
  func(f); // invoke mem on a copy of f
  func(&f); // invoke mem on f through a pointer
}

为什么我们不需要 std :: thread 时, mem_fun
通过重载自动检测这些情况,并且
自己正确的东西。

Why don't we need the mem_fun when constructing std::thread? It automatically detects those situations through an overload and does the right thing all by itself.

这篇关于std :: thread定义线程函数时的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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