Boost logo

Boost Users :

Subject: Re: [Boost-users] Several novice questions concerning threading
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-04-11 11:08:32


Le 11/04/12 13:31, Master a écrit :
> Hello all .
> i am a newbie to the boost community . i recently started learning
> about threads in boost . now there are some questions i would like to
> ask :
Welcome.
> 1.where can i find examples showing practical uses of boost::thread
> features?
The documentation doesn't contains too much examples. You can take a
look at the libs/thread/example and tutorial directories :(
> 2.how can i get all threads ID issued by me in my app?
No direct way other that storing them in a container. What is your use case?
> 3.how can i iterate through running threads in my app ?
No direct way other than storing a thread pointer in a container. What
is your use case?
> 4.is <http://4.is> there any kind of means to get all the running
> threads using boost library? if it does whats the calss? if it doesnt
> how can i do that?
See above. I think that you need to specialize the thread class so that
it inserts a handle to the created thread on a container at construction
time and remove it at destruction time.
> 5.can i resume a thread after pausing it ? ( how can i pause a thread? )
Boost.Thread doesn't provide fibers or resumable threads. There is
Boost.Fiber for that purpose (not yet in Boost).
> 6. how can i share a variable between two or more threads , suppose i
> have a loop , i want two threads to simultaneously iterate through it
> , if thread1 counted to 3, thread2 continues it from 4 and so on . ?
> i already tried
You need to protect the access to the loop index variable 'i' with a
mutex as you did with sum.

HTH,
Vicente
> ------
>
> what is wrong with my sample app ?
> #include <iostream>
> #include <boost/thread.hpp>
> using namespace std;
> using namespace boost;
>
> mutex bmutex;
> int i=0;
> int sum=0;
> void IteratorFunc(int threadid)
> {
> for ( ; i<25 ; i++)
> {
> lock_guard<mutex> locker(bmutex);
>
> cout<<"\t"<<threadid<<"\t"<<this_thread::get_id()<<"\t"<<i<<"\n";
> sum+=i;
> }
> }
>
> int main()
> {
> //boost::posix_time::ptime start =
> boost::posix_time::microsec_clock::local_time();
>
> thread thrd(IteratorFunc,1);
> thread thrd2(IteratorFunc,2);
>
> cout<<sum;
> thrd.join();
> thrd2.join();
> }
>
>



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