[英]Spawning threads inside a class
我收到一個與std::invoke
相關的編譯錯誤,以及另一個無法將模板專門用於invoke
的編譯錯誤。 這是代碼:
#pragma once
#include <thread>
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
class ThreadLoader {
private:
unsigned int id;
void _loadFile(const std::string& filepath) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Loading " << filepath << "\n";
}
public:
static unsigned int counter;
ThreadLoader() {
id = counter;
counter++;
}
void loadFile(const std::string& filepath) {
std::thread t(&ThreadLoader::_loadFile, std::ref(filepath), this);
t.join();
}
friend std::ostream& operator<<(std::ostream& os, const ThreadLoader& t) {
return os << t.id << std::endl;
}
};
unsigned int ThreadLoader::counter = 0;
當我嘗試調用loadFile
function 時出現錯誤。 我已經檢查過了。 我認為遵循公認的答案,但是錯誤仍然存在。 我認為我在std::thread t(&ThreadLoader::_loadFile, std::ref(filepath), this);
中做錯了什么 ,但我不知道它是什么。
你有你的參數錯誤的方式,它應該是:
std::thread t(&ThreadLoader::_loadFile, this, std::ref(filepath));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.