Questions about packaged_task , Promise and serialization of a class object and event handling

Hello all again . i have couple of questions again which i would appreciate any kind of help and answer, my first question is about packaged_task and how we can use it , i mean i tried using packaged_task with functions , with parameters , and i got errors doing it . so do we always have to use class or stcucts in order for us to use packaged_task ? how can i send some arguments to a function when im trying to make a package out of ? i have done the packaging with parameterelss functions , but i dont know how im supposed to pass parameters ! : here is my application working with parameterless functions .
------------------------- can someone show me a simple sample of using Promise in threading ? i mean i already know about future , whats the need for Promise ? ! -------------------------- whats the different between read_some and async_read_some ? in asio ? -------------------------- i know that when i need to send an argument to event handler ( signal ) , i frist should declare sth like this :
now if i have couple of functions with the same signature , doing different jobs with different arguments , how can i give their respective arguments ? i mean at the moment , i can only write : mysignal(5);! and this passes 5 to all functions connected with this signal! do i really have to make one signal for each functions ?so that i can pass different and yet with the same type argument to different functions using one signal . is'nt there any other way around it ? if inside a signal , i run a thread and then i block that signal using signal.block , would that block that thread too ? and then unblocking it would it unblock that thread too ? is it safe doing this ? --------------------------- why do we use function names with const that the end of it ? i mean sth like this :
what is that const for ? what does it declare ? why is it necessary to use it that way ? -------------------- how do i serialize a class of mine ? i mean i read that if i have vector , i need to use some functions , if i have foo i need another function ! what exactly do you serialize your classes ? i mean maybe i have std::string , char , vectors of some kind , and a self made class or type used in my class to be serialized , how should i go about it . any guidance is greatly appreciated . --------------------- i tired to use boost::container::vector for storing my threads , and it went just fine . but i have a problem in determining which thread is which , i mean i already found out that i can use if-else for this matter , but since i used a vector , and the number of threads being created at run-time is not known , so i need to use a for statement so that i can iterate throuh all of th evectors items and then if i find a match , i do as i wish . the problem is , when i try to have sth like this , it fails :
boost::id id = myvect[0]
i want to pass this id for matching operation ( or declare it as global ) , what am i missing again ? another problem i have in this regard is that , when i store my threads in a lets say vector just like the sample i gave earlier , it is not guaranteed that threads inside that vector actually points to a legal thread of execution by the time i go to check for sth .!( i mean there is a good chance that they already ran and got terminated!) seemingly i can not use joinable to see if i am dealing with an active or lets say legitimate thread ! , my question is , how can i check to see if an object of type thread , actually points to sth real ? how to check that ? --------------------- when i try to compile source codes using Asio using MingW , i get linking errors ! what is the cause and how can i fix that ? i get the errors:
Thank you all in advance Seyyed Hossein Hasan Pour

Le 15/04/12 18:57, Master a écrit :
template<typename R> class packaged_task { // execution void operator()(); } Waiting for template<class R, class... ArgTypes> class packaged_task<R(ArgTypes...)> { void operator()(ArgTypes... ); } you will need to use a functor.
Promise is the on of the basic class of the Boost.Thread/Futures design. In order to have a future you need a promise, a way to provide a value. packaged_task encapsulates a function with a promise and a future. When the functions ends the result is set on the promise so that you could retrieve it using the associated future. Bat see http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-par... for an example. for better explanation and a concrete example. Best, Vicente

First of all, please read thoroughly the following article: http://www.boost.org/community/policy.html
whats the different between read_some and async_read_some ? in asio ?
The former is synchronous, the latter is asynchronous. I recommend you to read the asio docs. http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio.html
I think it's not a technical issue, but a design one. A signal signals (sends some information) to all its slots. It cannot and should not distinguish between its slots. If your slots should receive different info from a signal, then probably they actually should connect *different* signals.
Have you read the documentation? http://www.boost.org/doc/libs/1_49_0/doc/html/signals2/tutorial.html#id31516... How is blocking a signal related to threads?
why do we use function names with const that the end of it ?
Unrelated to boost. http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10
Here you can find a trivial example, as well as more advanced ones: http://www.boost.org/doc/libs/1_49_0/libs/serialization/doc/tutorial.html#si...

Thank you all very much and sorry for anything wrong i have done :( . ok talking about threads ,how can i query a thread object to see if it points to a valid thread of execution ? will just comparing it with null! do it ? i mean specially after using join() or detach() on them ( i dont know if that was necessary!) question two : when i detach a thread, do i really loose it for good ? cant i have any access of any kind to it ? any kind of management ? checking it ? suppose my detached thread was to run a function named "somefunc()", will i access the thread in there ? i mean can i have any kind of management in that thread ? by management i mean get its id for example in case to differentiate it from other possible detached! or joined? threads running that specific function , and possibly do sth to the share resource that detached thread of mine were to work on , or simply change the owner ( for example create a new thread, move the ownership form the detached one to his new one , and then make it joinable !, may be this doesnt make sense , i just want to know if these kind of stuff are do able ) how detached threads of any use ? do i have to immediately write thread.join() in order for my thread to join with the main thread ? or i can do it at any place in my source code ? what about detaching it ? does it make any difference? do we have any means in pausing /resuming a thread ? rather than using the e.g windows api ? i mean suppose i have a thread thats done 90% of its work and suddenly and event is triggered and for some unknown duration , it needs to get paused , and then when the criteria is met , get resumed again , i cant simply delete that thread, because maybe it has done so much calculation on and its not a wise action to through everything and start over ! again because time is not specific , cant use sleep to block it for some time , so how should we do it ? is there a sample showing the use of sharedmemory with threading ? i already checked the interprocess section , there were two simple samples which didnt have any threading in it ! how would i want to use pointers in a multi threaded application ? i mean do i even need one of those smart pointers ? if i do, would you name some of scenarios that might need the use of smart pointers? i would be grateful again thank you :)

use joinable() member function: http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html#...
question two : when i detach a thread, do i really loose it for good ? cant i have any access of any kind to it ? any kind of management ? checking it ?
After you detach() the object from its thread, the object is no longer associated with the thread: http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html#... Of course, the thread continues to run.
suppose my detached thread was to run a function named "somefunc()", will i access the thread in there ?
If you mean using namespace this_thread, you can use its facilities even if there's no a thread object associated with this thread. You can also compare this_thread::get_id() to a stored thread::id object of another thread even if it's already detached from its object.
i mean can i have any kind of management in that thread ? by management i mean get its id for example in case to differentiate it from other possible detached! or joined?
No, you can't do virtually anything with a detached thread object. Detaching it means that you do not want to manage the underlying thread of execution by means of thread object.
do i have to immediately write thread.join() in order for my thread to join with the main thread ?
Calling join() on detached thread object won't have any effect: http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html#...
do we have any means in pausing /resuming a thread ?
You've got interruption point: http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html#...
rather than using the e.g windows api ?
Pausing/resuming threads with winapi is usually a bad idea (other than for debugging purposes), as you never know *where* it's paused. What can you do with such a thread?
It depends on the design of your program.

Thank you very much . does a thread of execution always have an id ? i mean does a threads id gets deleted when we make a thread detached ? im asking this because , if i detach a thread , and thus there is no valid boost::thread object available , how can i access a thread Id using boost::this_thread::get_id() ? the detached thread is not a boost::thread object any more right? then whats the point of comparing the id of this_thread ?! will it make sense at all ?

does a thread of execution always have an id ? i mean does a threads id gets deleted when we make a thread detached ?
It's not that thread id "gets deleted", but a detached thread object releases its internal thread info and thus it cannot return you thread id or any other thread-related info.
im asking this because , if i detach a thread , and thus there is no valid boost::thread object available , how can i access a thread Id using boost::this_thread::get_id() ?
This is because the facilities, which reside inside this_thread namespace, are available to you even if there's no thread object associated with this thread. You can even launch a thread using non-boost api (like _beginthread), and still be able to use this_thread functions (except for interruption).
the detached thread is not a boost::thread object any more right? then whats the point of comparing the id of this_thread ?! will it make sense at all ?
Yes, if you took thread.id(), stored it somewhere, called thread.detach(), you can later compare this id against this_thread::get_id().

Thank you very much for this wealth of information you kindly share with me :) i really really appreciate your help and time and wish you the best sir .
participants (3)
-
Igor R
-
Master
-
Vicente J. Botet Escriba