简体   繁体   English

如何将ifstream作为参数传递给std :: thread函数?

[英]How can I pass an ifstream as argument to std::thread function?

I did some research googling regarding the usage of std::ifstream within a thread as function argument, but managed to find its C implementation in pthread. 我对使用std::ifstream在线程中作为函数参数进行了一些研究,但设法在pthread中找到其C实现。 What I want is for it to be implemented in C++11, as I am using standard C++ threads. 我想要的是在C ++ 11中实现它,因为我正在使用标准C ++线程。

I am trying to pass a std::ifstream as argument to thread, but I get the following error: 我试图将std::ifstream作为参数传递给线程,但是出现以下错误:

error: no type named type in class std::result_of<void (*(std::basic_ifstream<char>))(std::basic_ifstream<char>)> 错误: class std::result_of<void (*(std::basic_ifstream<char>))(std::basic_ifstream<char>)>没有名为type
typedef typename result_of<_Callable(_Args...)>::type result_type;

How do I pass the ifstream argument to the function being called on thread? 如何将ifstream参数传递给线程上正在调用的函数? Here's my code: 这是我的代码:

#include <cstdlib>
#include <fstream>
#include <thread>
#include <iostream>
using namespace std;

void file_func_1(std::ifstream m_file)
{
    // ifstream m_file;
    string line;
    while (getline(m_file, line)) {
        cout << "the line now in is" << line
             << "from thread" << std::this_thread::get_id()
             << endl;
        std::this_thread::sleep_for(std::chrono::seconds((rand() % 10) + 1));
    }
}

void file_func_2(ifstream m_file)
{
    string line;
    while (getline(m_file, line)) {
        cout << "the line now in is" << line
             << "from thread" << std::this_thread::get_id()
             << endl;
        std::this_thread::sleep_for(std::chrono::seconds((rand() % 10) + 1));
    }
}

int main(int argc, char** argv)
{
    std::ifstream m_file;
    m_file.open("File_line.txt", ios::in);
    int name=8;
    std::thread func_th1(file_func_1, (m_file));
    // thread func_th2(file_func_2,m_file);
    func_th1.join();
    // func_th2.join();
    return 0;
}

You have two issues here. 您在这里有两个问题。 The first is that you can't pass streams by value, you need to pass them by reference. 首先是您不能按值传递流,需要按引用传递它们。

Second, it appears that you can't pass a stream via a thread directly (although there are probably work-arounds [EDIT: std::ref appears to work, despite comments - maybe it's a version issue as I'm using g++ with c++1z] ). 其次,似乎您无法直接通过线程传递流(尽管可能存在一些变通方法,尽管有评论,但[EDIT: std::ref似乎可以工作-在我使用g ++时c ++ 1z])。

So, make your functions access references, and call your functions using a lambda when creating the thread: 因此,使您的函数访问引用,并在创建线程时使用lambda调用函数:

#include <cstdlib>
#include <fstream>
#include <thread>
#include <iostream>
#include <functional>

using namespace std;

void file_func_1(std::ifstream &m_file){
    string line;
    while ( getline (m_file,line) ){
        cout<<"the line now in is"<<line<<"from thread"<<std::this_thread::get_id()<<endl;
        std::this_thread::sleep_for(std::chrono::seconds((rand() % 10) + 1));
    }
}

int main(int argc, char** argv) {
    std::ifstream m_file;
    m_file.open("File_line.txt",ios::in);
    int name=8;
    std::thread func_th1([&m_file]() { file_func_1(m_file); });
    std::thread func_th2(file_func_1, ref(m_file));
    func_th1.join();
    func_th2.join();
    return 0;
}

And don't forget that only one thread at a time should access the stream, so wrap access in a mutex of some sort. 并且不要忘记一次只能有一个线程访问该流,因此请将访问包装成某种互斥体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM