0

I am trying to create multiple threads in my program and whenever I try to do this

std::thread thread1(threadedfunction);

I get this error: non-standard syntax; use '&' to create a pointer to member

and when I put the & in like this:

std::thread thread1(&threadedfunction);

I got this error: '&': illegal operation on bound member function expression

PavelF
  • 321
  • 1
  • 2
  • 11
  • You need to also pass the object that the member function will act upon. Otherwise how can it know? – Paul Rooney Sep 16 '16 at 04:27
  • @PaulRooney what do you mean? – PavelF Sep 16 '16 at 22:43
  • You can't call a member function without an instance of that class. For example what would `thread::join` mean without a thread instance? – Paul Rooney Sep 16 '16 at 22:48
  • Ohhhhh, so I have to call it like this std::thread thread1(&class:threadedfunction) ? and then thread1::join() – PavelF Sep 16 '16 at 22:50
  • No maybe thread::join was a bad example. Let's use something different. Say class `X` has a member function `run`. You can't just call. `X::run()`. You need to do `X x; x.run();`. Where little x is the instance. If you wanted to launch this as a thread therefore you'd need to pass `x` to thread as well as the function, to give context e.g. `std::thread(&X::run, x);` – Paul Rooney Sep 16 '16 at 22:54
  • @PaulRooney thanks, I got it now. – PavelF Sep 16 '16 at 23:11
  • @PaulRooney sorry for bothering you, but now I have a separate problem, I'm trying to take input and output at the same time (hence the mutlithreading) but whenever I run input in another thread, It doesn't take any input – PavelF Sep 16 '16 at 23:37
  • your best bet is to ask a new question. I'm not 100% sure what you are asking, but if you can show code it would be clearer. – Paul Rooney Sep 16 '16 at 23:40

0 Answers0