将多个参数从函数传递到线程时遇到问题

时间:2016-11-02 00:39:07

标签: c++ multithreading

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

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

#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(&Self::tempEmotions, s, e, temp, i, ref(s));

我曾尝试在几个地方寻找如何通过几个阶级论证来完成这项工作,但我似乎无法找到答案。第二个线程工作,所以我相信它与参数有关。

编辑(11/8/16):我试图在源文件和int main()文件中移动参数,但它不起作用。我还有这个问题。我也尝试创建一个新的Self类并引用它,但它也没有用。     另外 - 当我尝试通过按&#34来分隔代码行中的每个参数时,输入&#34;,代码错误将归结为最后一部分; );存在相同的错误。

2 个答案:

答案 0 :(得分:1)

你的tempEmotions定义是什么?

成员函数的第一个参数始终是指向SELECT employer, (200 * 6) - sum(amount) as balance, sum(amount) / 200 as months_due FROM table WHERE AP_FROM => '2016-01-01' AND AP_To <= '2016-06-30' HAVING (200 * 6) - sum(amount) > 0;(您的对象)的指针,除非它是一个静态函数。

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

答案 1 :(得分:0)

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

#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。如果有人能解释为什么会发生这种情况,我们将不胜感激。目前,主文件中的单独类是临时修复。