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.
Produces the following set of errors (first couple ones):
`/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple