Lambda的变量不在范围内

问题描述 投票:0回答:3

我正在尝试诊断多核处理器[Xeon Silver]上的计时问题。我认为时钟尚未在处理器之间配置或同步。我正在使用Eli Bendersky的[在代码片段中得到认可的]线程示例来构建测试仪器。我做了三处更改。我首先使睡眠发生,然后添加了对std :: chrono :: system_clock :: now()的调用,并尝试将其打印出来。我正在CentOS 7.5上使用gcc 4.8.5进行构建。

代码如下:

// // Eli Bendersky [http://eli.thegreenplace.net]
// This code is in the public domain.
#include <algorithm>
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <pthread.h>

int main(int argc, const char** argv) {
  unsigned num_cpus = std::thread::hardware_concurrency();
  std::cout << "Launching " << num_cpus << " threads\n";

  // A mutex ensures orderly access to std::cout from multiple threads.
  std::mutex iomutex;
  std::vector<std::thread> threads(num_cpus);
  for (unsigned i = 0; i < num_cpus; ++i)
  {
    threads[i] = std::thread([&iomutex, i]
    {
       // Simulate important work done by the tread by sleeping for a bit...
      std::this_thread::sleep_for(std::chrono::milliseconds(200));
      {
         std::chrono::time_point ti = std::chrono::system_clock::now();
         // Use a lexical scope and lock_guard to safely lock the mutex only for
         // the duration of std::cout usage.
         std::lock_guard<std::mutex> iolock(iomutex);
         std::cout << "Thread #" << i << " hit its clock at: " << ti.time_since_epoch() << "\n";
       }
    });
  }

  for (auto& t : threads) {
      t.join();
  }
  return 0;
}

我使用make构建:

CXX = g++
CXXFLAGS = -std=c++11 -Wall -O3 -g -DNDEBUG -pthread
LDFLAGS = -lpthread -pthread

clock-check: clock-check.cpp
    $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)

GCC给我以下错误:

[user@sbc1 concur]$ make clock-check
g++ -std=c++11 -Wall -O3 -g -DNDEBUG -pthread clock-check.cpp -o clock-check -lpthread -pthread
clock-check.cpp: In lambda function:
clock-check.cpp:32:67: error: ‘ti’ was not declared in this scope
      std::cout << "Thread #" << i << " hit its clock at: " << ti.time_since_epoch() << "\n";
                                                               ^
make: *** [clock-check] Error 1

ti显然与print语句在同一块范围内,而我困惑于编译器为何抱怨。我尚未找到对lambda局部变量的任何限制。我发现的大多数内容都是对捕获的引用。

c++ lambda clock
3个回答
1
投票

您的问题在于此行:

std::chrono::time_point ti = std::chrono::system_clock::now();

[std::chrono::time_point需要类型自变量(例如std::chrono::time_point<std::chrono::system_clock>

在这种情况下,首选使用auto

auto ti = std::chrono::system_clock::now();

然后,由于尝试在输出流中输出std::chrono::duration,因此会出现错误。

您应该这样做:

std::cout << "Thread #" << i << " hit its clock at: " << ti.time_since_epoch().count() << "\n";

1
投票

这似乎是旧版gcc中的错误。使用gcc 10.1(--std = c ++ 11),我得到了error

<source>: In lambda function:   
<source>:23:34: error: missing template arguments before 'ti'    
   23 |          std::chrono::time_point ti = std::chrono::system_clock::now();    
      |                                  ^~    
<source>:27:67: error: 'ti' was not declared in this scope; did you mean 'i'?    
   27 |          std::cout << "Thread #" << i << " hit its clock at: " << ti.time_since_epoch() << "\n";    
      |                                                                   ^~    
      |            

关于声明中缺少模板参数的错误(gcc 4.5.8缺少该错误说明了第二个错误。

如果使用std::cout删除行,[https://godbolt.org/z/6LREHF],则带有-std = c ++ 11的gcc 4.8.5会愉快地编译代码>


0
投票

类模板推论在c ++ 17之前不可用,因此您需要为chrono::timepoint指定模板参数。或者,使用自动:

© www.soinside.com 2019 - 2024. All rights reserved.