Running a function on the main thread from a boost thread and passing parameters to that function
Asked Answered
B

1

1

I have some code running in a boost thread that modifies stuff handled by the main thread which is not working and it makes sense.

On android i would have the Handler which is a message queue that would execute my code on the main thread and i can pass whatever parameters i want to this handler.

I want to do the same with boost

so on my main thread i do the following:

boost::thread workerThread(boost::bind(&SomeClass::pollService, this));

My pollService method:

SomeClass::pollService()
{
     //get some stuff from a web service
     //parse the json response
     //NEEDED part: call a function to be executed on the main thread and hand it some functions
}

P.S. I have looked at many io_service.post examples and i still have no clue how to do it, and also i read an answer that said to use asio strand but i am also unable to understand it.

Can some one please dumb it down for me ? Please don't write something so abstract that i won't understand, I am not experienced in this. Thank you

Blighter answered 3/12, 2012 at 9:54 Comment(0)
H
5

Yes, io_service::post() is a convenient facility to post a functor from one thread to another, but the destination thread should execute io_service::run(), which is blocking function (it's kind of io_service "message loop"). So, assuming your main thread looks like this:

int main()
{
  // do some preparations, launch other threads...
  // ...
  io_service io;
  io.run();
}

...and assuming you've got an access to io object from pollService running in another thread, you can do the following:

SomeClass::pollService()
{
  // do something...
  // ...
  io.post([=] { doStuffThatShoudRunInMainThread(); });
}

If your compiler doesn't support c++11 lambdas, use bind -- but note that post expects nullary functor, i.e. a function-object that doesn't accept parameters.

Horde answered 3/12, 2012 at 11:0 Comment(6)
So how do I pass on parameters to my function ?Blighter
Just a note, i already fixed my problem by making an std:queue and putting stuff in it and while(!queue.empty()) i do stuff in the gui thread which already loops every tick, and it is working fine, i need to know just to know how to do it right in the futureBlighter
@Shereef if you use lambda syntax - pass the params directly to doStuffThatShoudRunInMainThread(), otherwise bind the parameters when creating the functor: bind(&doStuffThatShoudRunInMainThread, arg1, arg2, arg3). Of course, if it's a member function you should bind it with this or better with shared_from_this.Horde
Can you please take a look at https://mcmap.net/q/1483764/-using-boost-asio-io_service-post/435706 , Thank youBlighter
void funtionToCreateThread() { //code to update ui //create thread //thread join} here thread join code blocks update ui calls at the begining, how to fix this ? Since ioserver run is also a blocking call i feel that first comment code wont work. can you suggest how to get by this ?Givens
@Satyam Raikar I think it's worth creating a new question.Horde

© 2022 - 2024 — McMap. All rights reserved.