0
FirstStructure first = {};
SecondStructure second = {};
char buffer[1024] = {};

second.init(); // set some attributes

int fd = second.get();

// L9: first = second.getMore(fd);

// L11: std::thread t1(&SecondStructure::getMore, &first, fd);

// printf("%d\n", first.firstAtr);
// printf("%d\n", first.secAtr);

I'd like to execute L9 with thread.

On the L11 is some of my trying, it doesn't work, so feel free to ignore it.

Thanks for some help!

  • You need to post the signature of the `getMore` function, you seem to be calling it differently on L9 and L11. – nick Mar 22 '20 at 22:14
  • 1
    Will it work if you add `t1.join()` after L11? Upd: maybe you meant `second` on L11, like `std::thread t1(&SecondStructure::getMore, &second, fd);`? – fas Mar 22 '20 at 22:15
  • @nick I am a bit confused, could you give me a little example or more hints? – John Doe Mar 22 '20 at 22:18
  • @user3365922 Maybe, but how can I store the output to `first`? – John Doe Mar 22 '20 at 22:19
  • It would be helpful if you provided the signature for this function `SecondStructure::getMore` i.e. something like `void SecondStructure::getMore(FirstStructure*, int) {...}` But you should try `t1.join()` first, that is a much better idea. – nick Mar 22 '20 at 22:20
  • I thought it is clear from `L9`, I am sorry. It looks like `int SecondStructure::getMore(int fd)`. – John Doe Mar 22 '20 at 22:22
  • 2
    See [this question](https://stackoverflow.com/questions/10673585/start-thread-with-member-function). – 1201ProgramAlarm Mar 22 '20 at 22:27

1 Answers1

-1

The easiest way is to pass a lambda to your thread. Something like

std::thread t1([&](){ first = second.getMore(fd); });

Make sure you join the thread at some appropriate time later.

super
  • 12,335
  • 2
  • 19
  • 29