【问题标题】:Having problems passing multiple arguments from a function into a thread将多个参数从函数传递到线程时遇到问题
【发布时间】:2016-11-11 10:07:19
【问题描述】:

我希望这不是一个愚蠢的问题。请不要投反对票,我是多线程的初学者。

在将参数从类中的函数写入线程时遇到问题。在这里。

#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<fstream>

#include<thread>

#include<sstream>

#include<stdio.h>
#include<string.h>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<stdlib.h>

#include <ctime>
//
#include<atomic>
#include<functional>
//
#include <unistd.h>
#include<time.h>

#include"Directory.hpp"
#include"Tests.hpp"
//#define NUM_THREADS 2

using namespace std;

/*
template<class T> void f(T)
void addThreadNoArgs(T) {
    thread
}
*/

int main() {
    Emotions e;
    DictObj d;
    //Time t;
    //User u;
    Self s;
    Tests test;
    vector < thread > threads;

    int i = 200;


    Emotions temp;

    //pthread_t threads[NUM_THREADS];
    //-----------------------------------------------
    e.setEmo(50, 50, 25, 50, 40, 50, 30, 20, 10, 20);
    temp.setEmo(0,0,0,0,0,0,0,0,0,0);

    s.setEmotions(e);
    s.setTempEmo(temp);



    /*
    int rc;
    int tc;
    rc = pthread_create(&threads[0], NULL, s.tempEmotions, s, e, temp, 200);
    tc = pthread_create(&threads[1], NULL, test.testEmoALL, NULL);
    */
    //----------------------------------------------
    //threads.push_back(thread(&Self::tempEmotions,s,e,temp,200));
    thread first(&Self::tempEmotions, s, e, temp, i, ref(s));
    thread second(&Tests::testEmoALL, ref(test));
    //first.join();
    //second.join();

    cout << endl << "First and second completed";


    //first.detach();
    //second.detach();
    //----------------------------------------------

    return 0;
}

错误

g++ -std=c++11 -Wall -c "NO-DELETE.cpp" -lpthread (in directory: /home/courtneymaroney/Desktop/Courtney/Documents/AI/NEW)
In file included from /usr/include/c++/5/thread:39:0,
                 from NO-DELETE.cpp:7:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (Self::*)(Self, Emotions, Emotions, int)>(Self, Emotions, Emotions, int, std::reference_wrapper<Self>)>’:
/usr/include/c++/5/thread:137:59:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Self::*)(Self, Emotions, Emotions, int); _Args = {Self&, Emotions&, Emotions&, int&, std::reference_wrapper<Self>}]’
NO-DELETE.cpp:71:57:   required from here
/usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Self::*)(Self, Emotions, Emotions, int)>(Self, Emotions, Emotions, int, std::reference_wrapper<Self>)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Self::*)(Self, Emotions, Emotions, int)>(Self, Emotions, Emotions, int, std::reference_wrapper<Self>)>’
         _M_invoke(_Index_tuple<_Indices...>)
         ^
Compilation failed.

导致问题的行是以下行:
thread first(&amp;Self::tempEmotions, s, e, temp, i, ref(s));

我已经尝试在几个地方寻找如何使用几个类参数来解决这个问题,但我似乎找不到答案。线程第二个工作,所以我相信它与论点有关。

编辑(11/8/16):我尝试在源文件和int main() 文件中移动参数,但它不起作用。我仍然有这个问题。我也尝试创建一个新的 Self 类并引用它,但它也没有工作。 ALSO——当我尝试通过按“enter”来分隔代码行中的每个参数时,代码错误被带到最后一部分; ); 存在同样的错误。

【问题讨论】:

    标签: c++ multithreading


    【解决方案1】:

    你对 tempEmotions 的定义是什么?

    成员函数的第一个参数始终是指向this(您的对象)的指针,除非它是静态函数。

    How do I pass an instance member function as callback to std::thread

    【讨论】:

    • void tempEmotions(Self robot, Emotions original, Emotioms temp, int seconds)
    • 问题出在最后一个参数:thread first(&amp;Self::tempEmotions, s, e, temp, i, ref(s)); 你正试图发送对 s 的引用,但 tempEmotions 函数不想要它。
    • 如果我尝试没有 ref(s),它说这是错误的参数数量并且仍然给我错误。你能帮我解决这个问题吗?我应该发布与此相关的错误等吗?
    • 尝试清理。创建一个仅包含您担心的类/函数的 .cpp 新文件。
    • 继续我上面的评论...在该源文件上编译时发布错误。只试验那个源文件。尝试更改参数顺序,看看编译错误如何变化。
    【解决方案2】:

    我仍然不确定出了什么问题,但我在文件中创建了一个单独的类,并以某种方式解决了它。我确信有更好的解决方案,但这是一个临时解决方案,直到有人可以进一步帮助我。这就是我所做的:

    #include<iostream>
    #include<iomanip>
    #include<vector>
    #include<string
    #include<fstream>
    
    #include<thread>
    
    #include<sstream>
    
    #include<stdio.h>
    #include<string.h>
    #include<cstdlib>
    #include<cctype>
    #include<algorithm>
    #include<stdlib.h>
    
    #include <ctime>
    //
    //#include<atomic>
    //
    #include <unistd.h>
    #include<time.h>
    
    #include"Directory.hpp"
    #include"Tests.hpp"
    //#define NUM_THREADS 2
    
    using namespace std;
    
    /*
    template<typename _Tp>
    class T {
        public:
            _Tp getT() { return t;}
        private: 
            _Tp t;
        };
        */
    class RunF {
        private:
                Emotions e;
            DictObj d;
            Self s;
            Tests test;
            Emotions temp;
        public:
            void setE(Emotions emo) {e = emo;}
            void setTE(Emotions emo) {temp = emo;}
            void setS(Self emo) {s= emo;}
            void setT(Tests emo) {test = emo;}
            void setD(DictObj emo) { d = emo;}
            //----------------------
            void function0() { test.testEmoALL(); }
            void function1() {
                s.tempEmotions(200, e, temp, s);
            }
    };
    int main() {
    
    Emotions e;
    DictObj d;
    //Time t;
    User u;
    Self s;
    Tests test;
    vector < thread > threads;
    Emotions temp;
    
    
    
    //Emotions temp;
    
    //pthread_t threads[NUM_THREADS];
    //-----------------------------------------------
    e.setEmo(50, 50, 25, 50, 40, 50, 30, 20, 10, 20);
    temp.setEmo(0,0,0,0,0,0,0,0,0,0);
    
    s.setEmotions(e);
    s.setTempEmo(temp);
    
    //const Self &testSelf();
    /*
    int rc;
    int tc;
    rc = pthread_create(&threads[0], NULL, s.tempEmotions, s, e, temp, 200);
    tc = pthread_create(&threads[1], NULL, test.testEmoALL, NULL);
    */
    //----------------------------------------------
    //threads.push_back(thread(&Self::tempEmotions,s,e,temp,200));
    //--------
    RunF f;
    f.setE(e);
    f.setTE(temp);
    f.setS(s);
    f.setT(test);
    f.setD(d);
    //--------
    thread first(&RunF::function1, ref(f));
    thread second(&Tests::testEmoALL, ref(test));
    first.join();
    second.join();
    
    cout << endl << "First and second completed";
    
    
    //first.detach();
    second.detach();
    //----------------------------------------------
    
    return 0;
    }
    

    RunF 类是我将函数定义为文件的一种本地类的地方,而不是将文件中的类从目录直接导入主线程。我不确定为什么会这样,但我怀疑这是因为文件中的本地类和不同的文件可能存在一些脱节。我还可以发现,我的目录文件可以编译,但它没有构建,并说它需要一个main。如果有人能解释为什么会发生这种情况,将不胜感激。目前,主文件中的单独类是一个临时修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      相关资源
      最近更新 更多