Reputation: 90
I hope this isn't a stupid question. Please do not vote this down, I am a beginner with multithreading.
I've come into a problem when writing arguments into a thread from a function from a class. Here it is.
#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;
}
Errors
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.
The line that's causing the problem is with the following line:
thread first(&Self::tempEmotions, s, e, temp, i, ref(s));
I've tried looking in several places to figure out how to make this work with several class-arguments, but I can't seem to find the answer. thread second works though, so I am convinced it has to do with the arguments.
EDIT (11/8/16): I tried to move the arguments around in both the source file and the int main()
file, but it did not work. I am still having this issue. I also tried to create a new Self class and reference it to that, but it also did not work.
ALSO-- When I try to separate each argument in the line of code by pressing "enter", the code error is brought down to the last part; );
The same errors exist.
Upvotes: 1
Views: 654
Reputation: 90
I'm still not exactly sure what went wrong, but I created a separate class within the file, and got around it somehow. I'm sure there's a better solution, but this is a temporary fix until someone can help me further. This is what I did:
#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;
}
The class, RunF, is where I define the functions as a kind of local class to the file, rather than importing the classes from the file from the directory right into the main thread. I'm not sure why this works, but I suspect it's because there may be some disconnect with local classes in the file and different files. I can also find that, my directory file compiles, but it does not build, and says it needs a main
. If someone can explain why this happens, it would be greatly appreciated. For now, the separate class in the main file is a temporary fix.
Upvotes: 0
Reputation: 60
What is your tempEmotions definition?
The first argument to a member function is always a pointer to this
(your object), unless it's a static func.
How do I pass an instance member function as callback to std::thread
Upvotes: 1