|
Boost Users : |
Subject: [Boost-users] Questions about packaged_task , Promise and serialization of a class object and event handling
From: Master (master.huricane_at_[hidden])
Date: 2012-04-15 12:57:31
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 .
> //in the name of GOD
> //Seyyed Hossein Hasan Pour
> //Learning Boost- How Threading works in Boost
> //How to pass parameters to functions in packaged_task
> #define BOOST_THREAD_USE_LIB
> #include <iostream>
> #include <cstdlib>
> #include <boost/thread.hpp>
> #include <boost/date_time/posix_time/posix_time.hpp>
> using namespace std;
>
> vector<int> SharedResource;
> boost::shared_mutex sharedMutex;
>
> int FillSharedResource()
> {
> int numberOfItems=10;
> for (int i = 0; i< numberOfItems; i++)
> {
> boost::unique_lock<boost::shared_mutex> lock(sharedMutex);
> SharedResource.push_back(i);
> lock.unlock();
> }
> return 10;
> }
>
> int Print1()
> {
> for (int i = 0; i< SharedResource.size(); i++)
> {
> boost::this_thread::sleep(boost::posix_time::seconds(1));
> boost::lock_guard<boost::shared_mutex> locker(sharedMutex);
> cout<<boost::this_thread::get_id()<<"\tItem:
> "<<SharedResource[i]<<endl;
> }
> return 101;
> }
>
> int Print2()
> {
> for (int i = 0; i< SharedResource.size(); i++)
> {
> boost::this_thread::sleep(boost::posix_time::seconds(1));
> boost::lock_guard<boost::shared_mutex> locker(sharedMutex);
> cout<<boost::this_thread::get_id()<<"\tItem:
> "<<SharedResource[i]<<endl;
> }
> return 102;
> }
>
> int main()
> {
>
> boost::packaged_task<int>
> pkg0(FillSharedResource),
> pkg1(Print1),
> pkg2(Print2);
>
> boost::thread
> t0(ref(pkg0)),
> t1(ref(pkg1)),
> t2(ref(pkg2));
>
> boost::unique_future<int>
> f0(pkg0.get_future()),
> f1(pkg1.get_future()),
> f2(pkg2.get_future());
>
> std::cout<< f0.get()<<"\n"<< f1.get()<< "\n"<< f2.get();
>
> system("pause");
> return 0;
> }
>
-------------------------
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 :
> *boost::signal2::signal <function-return-type (paramerter type)> mysignal
> ;*
> *mysignal**.connect(myfunc,my-functions-argumant)*;
> *mysignal**.connect(myfunc2,my-function2s-argumant)*;
>
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 :
> void operator()() const
> {
> }
> or
> void boostFunctionName(sth) const
> {
> }
>
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:
> C:\boost_1_49_0\boost\asio\detail\impl\winsock_init.ipp|39|undefined
> reference to `WSAStartup_at_8'|
> C:\boost_1_49_0\boost\asio\detail\impl\winsock_init.ipp|48|undefined
> reference to `WSACleanup_at_0'|
>
Thank you all in advance
Seyyed Hossein Hasan Pour
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net