Hello Master,

(sounds a bit weired if you ask beginners questions ;)

On Wed, Apr 11, 2012 at 5:08 PM, Vicente J. Botet Escriba <vicente.botet@wanadoo.fr> wrote:
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 :(

I would suggest reading Anthony's book on C++ concurrency. The threads in C++11 were modeled after Boost.Threads and Anthony gave the Boost.Threads library a major rewrite. Here is a link to it: http://www.manning.com/williams/ This book contains lots of examples which are explained in great detail.

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 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).

Just some thought on that... He did not directly ask about fibers... Pausing threads is considered a fragile design and bad practice. What happens if the thread holding a mutex is paused and the thread which should resume it, wants a mutex => deadlock... For example, newer .NET Framework versions depricated pause/resume functionality because of such behavior.


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


Best Regards,
Ovanes 
------
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();
}