Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么在C++;线程函数调用?_C++_Multithreading_Threadpool - Fatal编程技术网

C++ 为什么在C++;线程函数调用?

C++ 为什么在C++;线程函数调用?,c++,multithreading,threadpool,C++,Multithreading,Threadpool,我很好奇我到底做错了什么。我有以下功能: 索引器h: class DtIndexer { public: static void ThreadedIndex(string folderPath); indexer.cpp void DtIndexer::ThreadedIndex(string folderPath) { cout << "\t-> Indexing Folder: " << folderPath << endl;

我很好奇我到底做错了什么。我有以下功能:

索引器h:

class DtIndexer {
public:
    static void ThreadedIndex(string folderPath);
indexer.cpp

void DtIndexer::ThreadedIndex(string folderPath) {
    cout << "\t-> Indexing Folder: " << folderPath << endl;
    cout << "\t->> Done..." << endl;
}
我得到:

此处需要/usr/include/c++/6/函数:1286:7:错误:静态 断言失败:指向成员的指针的参数数目错误 静态断言(_Varargs::value)

对于C++,我还是相当新的,所以,任何建议都会被赏识。< /P> < P>替换< /P>
const char *folder = GetFolderPath(s, data.IncludeSubFolders);

线程从按值复制参数开始,然后复制一个
const char*
,它可能指向
GetFolderPath
函数中的一个静态缓冲区。该缓冲区将在每次调用时被覆盖,因此从中读取的线程可能会或可能不会得到您期望的结果

注意,您不需要执行
emplace\u back(std::thread)(…
因为作为参数写入
emplace_back
的内容被转发到
向量所持有的类型的构造函数,在本例中是
std::thread
。除非您真的愿意,否则也不需要将线程函数
设为static
。您可以从lambda开始,捕获
thread是
文件夹。您可能注意到的另一件事是,当多个线程同时写入
std::cout
时,输出会变得混乱。您可以保护公共资源(如
std::cout
)使用
std::mutex
std::lock\u guard
防止多线程同时访问

这是一个类的版本,其中包含一个非静态线程方法和一个用于
std::cout
:

#include <iostream>
#include <vector>
#include <cstring>
#include <mutex>
#include <thread>

const char* GetFolderPath() {
    static std::vector<std::string> paths = {"this is a path", "here is another one",
                                             "what do we have here", "mother of dirs",
                                             "what did the tomatoe ..."};
    static size_t idx = 0;
    static char buf[256];
    std::strcpy(buf, paths[idx].c_str());
    idx = (idx + 1) % paths.size();
    return buf;
}

class DtIndexer {
    std::mutex mtx_cout; // std::cout mutex

public:
    void ThreadedIndex(std::string folderPath) {
        std::lock_guard lock(mtx_cout); // wait until this thread acquires the mutex
        std::cout << "\t-> Indexing Folder: " << folderPath << "\n";
    }

    void UpdateIndex() {
        std::vector<std::thread> threadList;

        { // lock_guard scope
            std::lock_guard lock(mtx_cout); // acquire mutex

            for(int i = 0; i < 50; ++i) {
                // const char* folder = GetFolderPath();
                std::string folder(GetFolderPath());

                std::cout << "\t-> Adding Folder to Thread: " << folder << "\n";

                //             safe copy here --+       +--- thread runs here ---+
                //                              |       |                        |
                //                              V       V                        V
                threadList.emplace_back( [this, folder] { ThreadedIndex(folder); });
                //                      Δ
                //                      |
                //                      +-- no std::thread(... needed here
            }
        } // lock_guard releases mutex here

        for(auto& t : threadList) t.join();
    }
};

int main() {
    DtIndexer a;
    a.UpdateIndex();
}
#包括
#包括
#包括
#包括
#包括
常量char*GetFolderPath(){
静态std::vector path={“这是一条路径”,“这是另一条路径”,
“我们这里有什么”,“迪尔斯之母”,
“西红柿干了什么……”;
静态大小\u t idx=0;
静态字符buf[256];
std::strcpy(buf,路径[idx].c_str());
idx=(idx+1)%path.size();
返回buf;
}
类索引器{
std::mutex mtx_cout;//std::cout mutex
公众:
void ThreadedIndex(std::string folderPath){
std::lock\u guard lock(mtx\u cout);//等待该线程获取互斥锁

std::cout
GetFolderPath
做什么?当非
静态
时,必须在
DtIndexer
实例上调用
DtIndexer::ThreadedIndex
以获取有效的
this
。该实例是您缺少的参数。如果不需要
this
,最好保持
静态
或使用。您知道吗这一行中的t
thread(ThreadedIndex,folder)
称为
thread(&ThreadedIndex,const char*)
第二个参数是指局部变量,当for循环的迭代结束时,字符串被销毁。因此,当线程体被执行时,字符串
folderPath
是从悬空指针创建的。您应该按值传递字符串,而不是
const char*
传递给线程。@rafix07是的,谢谢。这就是问题所在。我也要这样做ing希望查看用户4581301添加的信息。再次感谢所有人。如果您想将此作为答案(针对我的具体问题),我会将其标记为已接受。您的问题缺少a。谢谢,这非常有意义,并且与其他评论的建议一致。我感谢您的详细解释。
const char *folder = GetFolderPath(s, data.IncludeSubFolders);
std::string folder( GetFolderPath(s, data.IncludeSubFolders) );
#include <iostream>
#include <vector>
#include <cstring>
#include <mutex>
#include <thread>

const char* GetFolderPath() {
    static std::vector<std::string> paths = {"this is a path", "here is another one",
                                             "what do we have here", "mother of dirs",
                                             "what did the tomatoe ..."};
    static size_t idx = 0;
    static char buf[256];
    std::strcpy(buf, paths[idx].c_str());
    idx = (idx + 1) % paths.size();
    return buf;
}

class DtIndexer {
    std::mutex mtx_cout; // std::cout mutex

public:
    void ThreadedIndex(std::string folderPath) {
        std::lock_guard lock(mtx_cout); // wait until this thread acquires the mutex
        std::cout << "\t-> Indexing Folder: " << folderPath << "\n";
    }

    void UpdateIndex() {
        std::vector<std::thread> threadList;

        { // lock_guard scope
            std::lock_guard lock(mtx_cout); // acquire mutex

            for(int i = 0; i < 50; ++i) {
                // const char* folder = GetFolderPath();
                std::string folder(GetFolderPath());

                std::cout << "\t-> Adding Folder to Thread: " << folder << "\n";

                //             safe copy here --+       +--- thread runs here ---+
                //                              |       |                        |
                //                              V       V                        V
                threadList.emplace_back( [this, folder] { ThreadedIndex(folder); });
                //                      Δ
                //                      |
                //                      +-- no std::thread(... needed here
            }
        } // lock_guard releases mutex here

        for(auto& t : threadList) t.join();
    }
};

int main() {
    DtIndexer a;
    a.UpdateIndex();
}