<br><br><div class="gmail_quote">On Wed, Apr 11, 2012 at 6:39 PM, Master <span dir="ltr">&lt;<a href="mailto:master.huricane@gmail.com">master.huricane@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr">Thank you very much :)<br>for keeping track of my created threads i decided to use thread_groups and accumulate them there . i came up with sth like this :<br><br>������� boost::thread_group threadstore;<br>



������� <br>������� threadstore.add_thread(&amp;thread);<br>������� threadstore.add_thread(&amp;thread2);<br>������� BOOST_FOREACH(boost::thread t ,threadstore.threads)<br>������� {<br>����������� cout&lt;&lt;t.get_id();<br>



������� }<br><br></div></blockquote><div>As far as I can see the docs thread_group (<a href="http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html#thread.thread_management.threadgroup">http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html#thread.thread_management.threadgroup</a>)�does not expose the public member threads. Even if it is there, it is an implementation detail, which can change/(made private) with any release of boost and your code will break. I suggest you to implement an approache suggested by Vicente.</div>

<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br>well it didnt compiled ! before that , i tried using sth like this which failed nontheless .<br>

<br>������ vector&lt;boost::thread&gt; threadstore;<br>������� <br>������� threadstore.push_back(thread);<br>

������� threadstore.push_back(thread2);<br>������� BOOST_FOREACH(boost::thread t ,threadstore)<br>������� {<br>����������� cout&lt;&lt;t.get_id();<br>������� }<br><br></div></blockquote><div>Thread group is just a management construct, it does not allow you to iterate over the threads. BOOST_FOREACH supports STL like container. thread_group does not provide begin/end iterators.</div>

<div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br>im clueless of the cause .<br>The threadstore has member called� :threads , which i dont know how to work around it , there is also another member named: m ,<br>



�which i have no clue of what it is !nor i know where it came form! or what i can do with it!<br></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">

�i couldnt find any information on these members on the boost::thread documentation either<br></div></blockquote><div>This an implementation detailed. It should be transparent for you, don&#39;t use it.</div><div>�</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">for that example i posted , i moved the mutex inside the loop and then used a sleep() method for couple of microseconds and got it working ( i mean now both threads seem to work as i expected them).<br>

but i want to know if we have sth like , lets say a timed_lock kind of lock !, so that a thread would only hold a mutex for a specified time , and when the timeslice presented in the timed_lock() passes , the affordmentioned thread releases the mutex<br>

</div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">and thus other thread(s) can get that mutex and so there would be no need for a sleep() for a thread to wait till it time-slice finishes up and thus releases the mutex .the current timed_lock tries to obtain the mutex in a specified time , which is not the case .<br>

</div></blockquote><div>I think you mixup smth. here. Generally speaking there are critical regions in a parallel application. These regions must be protected by synchronization objects to grant only a single thread modification for that region. Now what you ask for: I know there is a long critical region, but in the middle of the critical region I want a break and another thread should run than. If it is so, make 2 regions, but there is no way to implement smth like that in that simple manner. May be transactions, that you interrupt the execution, roll back the calculated state, let others run and start the calculation again.</div>

<div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">

i remember i tried to use yield() for achieving such a possiblity (releasing mutex as soon as possible , i think it kinda worked ,i gave a similare result when i used sleep() )<br></div></blockquote><div>How should it help, if the other thread wants in the exact critical section which is locked? Yield just gives the remaining CPU time slice of the thread to the scheduler. And scheduler might decide who runs next. It can even happen, that this one thread runs again, if its prio the highest.</div>

<div><br></div><div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">here is the code which i wrote to actually speed up the sum action , which i think didnt give any speed ! would you see where the problem is ? <br>



//in the name of GOD<br>//Seyyed Hossein Hasan Pour<br>//Working with Boost::threads<br>#define BOOST_THREAD_USE_LIB<br>#include &lt;iostream&gt;<br>#include &lt;boost/date_time/posix_time/posix_time.hpp&gt;<div class="im">

<br>#include &lt;boost/thread.hpp&gt;<br>

using namespace std;<br><br></div>boost::uint64_t� i = 0;<br>boost::uint64_t� sum=0;<br>boost::mutex mutex;<br><br>void IteratorFunc()<br>{<br><br>��� for (i ; i&lt;100000; i++)<br>��� {<br>������� mutex.lock();<br>������� sum+=i;<br>



������ cout&lt;&lt;i&lt;&lt;&quot;\t&quot;&lt;&lt;boost::this_thread::get_id()&lt;&lt;endl;<br>������� mutex.unlock();<br>������� //boost::this_thread::sleep(boost::posix_time::microseconds(200));<br>������ boost::this_thread::yield();<br>



��� }<br><br>}<br><br>int main()<br>{<div class="im"><br>��� boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();<br></div>��� boost::thread thread(IteratorFunc);<br>��� boost::thread thread2(IteratorFunc);<br>

<br>

//��� boost::thread_group threadstore;<br>//<br>//��� threadstore.add_thread(&amp;thread);<br>//��� threadstore.add_thread(&amp;thread2);<br>//<br>//��� BOOST_FOREACH(boost::thread t ,threadstore.threads)<br>//��� {<br>//������� cout&lt;&lt;t.get_id();<br>



//��� }<br><br>��� boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();<br><br>��� thread.join();<br>��� thread2.join();<br><br>��� cout &lt;&lt; &quot;sum =\t&quot; &lt;&lt; sum&lt;&lt; &quot;\t&quot;&lt;&lt;end-start&lt;&lt;endl;<br>



��� return 0;<br>}<br></div></blockquote><div>The speed up would rely on the lock-free implementation and dividing of work into independent pieces. How sleep suppose to speedup smth. if in that time period is nothing calculaded? CPU just stands still. Sum can be speedup greatly. The keyword here is work stealing, first proposed in Cilk (<a href="http://en.wikipedia.org/wiki/Cilk">http://en.wikipedia.org/wiki/Cilk</a>). Intel acquired Cilk. For C++ you can use Intel Threading Building Blocks, which has a parallel_reduce algorithm. Just download the tutorial:�<a href="http://threadingbuildingblocks.org/uploads/81/91/Latest%20Open%20Source%20Documentation/Tutorial.pdf">http://threadingbuildingblocks.org/uploads/81/91/Latest%20Open%20Source%20Documentation/Tutorial.pdf</a> and take a look at chapter: 3.3.</div>

<div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br>i also want to know if we have such a capability where i can specify the priority of a thread or a group of thread against the other threads .<br>

</div></blockquote><div>You need to get the native handle and use the native OS functions.</div><div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">

let me explain it little more, by that i mean , suppose we have couple of reader threads and one or two writer threads , is there any kind of possiblity that i can grant the writer thread(s) more priority in terms of accessing<br>



a resource (by obtaining the mutex more often ? - or the writer thread(s) deny access to readers in some cases ? ) ? or a part of memory ? ( e.g an array of some kind ? )<br>if it is possible , how can i achieve such possibility ?<br>

</div></blockquote><div>Boost provides basic reader/writer lock concepts. For more finer grained concepts I think you will need implement this concept yourself. But usually reader/writer is implemented the way that if a writer wants to enter the critical section no other readers will enter it before the writer is done. And writer has to wait until all currently active readers are done. Actually the speedup here would be to increase the priority of readers to the priority of the writer.</div>

<div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">

is it possible that i can know if a thread was successful in doing what it was sent to ?<br></div></blockquote><div>Actually, you should decouple threads from work and consider using futures and thread pools. You are not interested in the thread&#39;s state, but the result which was calculated in parallel. So you wait until the result is available and verify it.</div>

<div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">can i specify that a thread or a group of thread execute in a specific order ? for example thread one must always execute first and thread two must always follow thread one . do we have such a thing ?<br>

</div></blockquote><div>Again, future pattern would help. You put work items in the queue and they are calculated in parallel but taken from the queue in a predefined order. You can also use a priority queue to sort the items according their priority when those are queued. On the other hand: You ask here for sequential execution. Why do you need threads than?</div>

<div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">

do i have any means of talking to threads ? checking the satus of a specific thread ? or group of threads ?<br></div></blockquote><div>What status would you like to check?</div><div>�</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr">can i know by any means , that which threads are blocked and which are not ?<br></div></blockquote><div>Blocked in terms of what? Waiting to enter the critical section or are currently not running?</div><div>

<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br>Thank you so much for your time and please excuse me for such newbish and yet long questions .<br>



i really do appreciate your help and time :)<br>Regards<br>Hossein<div><div class="h5"><br><br><br>On Wed, Apr 11, 2012 at 7:38 PM, Vicente J. Botet Escriba &lt;<a href="mailto:vicente.botet@wanadoo.fr" target="_blank">vicente.botet@wanadoo.fr</a>&gt; wrote:<br>



</div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    Le 11/04/12 13:31, Master a �crit�:
    <div><blockquote type="cite">
      <div dir="ltr">Hello all .<br>
        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 :<br>
      </div>
    </blockquote></div>
    Welcome.<div><br>
    <blockquote type="cite">
      <div dir="ltr">1.where can i find examples showing practical uses
        of boost::thread features? <br>
      </div>
    </blockquote></div>
    The documentation doesn&#39;t contains too much examples. You can take a
    look at the libs/thread/example and tutorial directories :(
    <div><blockquote type="cite">
      <div dir="ltr">
        2.how can i get all threads ID issued by me in my app? <br>
      </div>
    </blockquote></div>
    No direct way other that storing them in a container. What is your
    use case?<div><br>
    <blockquote type="cite">
      <div dir="ltr">
        3.how can i iterate through running threads in my app ? <br>
      </div>
    </blockquote></div>
    No direct way other than storing a thread pointer in a container.
    What is your use case?
    <div><blockquote type="cite">
      <div dir="ltr">
        <a href="http://4.is" target="_blank">4.is</a> 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?<br>
      </div>
    </blockquote></div>
    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.<div><br>
    <blockquote type="cite">
      <div dir="ltr">
        5.can i resume a thread after pausing it ? ( how can i pause a
        thread? )<br>
      </div>
    </blockquote></div>
    Boost.Thread doesn&#39;t provide fibers or resumable threads. There is
    Boost.Fiber for that purpose (not yet in Boost).<div><br>
    <blockquote type="cite">
      <div dir="ltr">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 . ?<br>
        i already tried� <br>
      </div>
    </blockquote></div>
    You need to protect the access to the loop index variable &#39;i&#39; with a
    mutex as you did with sum.<br>
    <br>
    HTH,<br>
    Vicente<div><br>
    <blockquote type="cite">
      <div dir="ltr">------<br>
        <blockquote style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">what
          is wrong with my sample app ? <br>
          #include &lt;iostream&gt;<br>
          #include &lt;boost/thread.hpp&gt;<br>
          using namespace std;<br>
          using namespace boost;<br>
          <br>
          mutex bmutex;<br>
          int i=0;<br>
          int sum=0;<br>
          void IteratorFunc(int threadid)<br>
          {<br>
          ������������ for (� ; i&lt;25 ; i++)<br>
          ����������� {<br>
          ������������������� lock_guard&lt;mutex&gt; locker(bmutex);<br>
          ��������������������
cout&lt;&lt;&quot;\t&quot;&lt;&lt;threadid&lt;&lt;&quot;\t&quot;&lt;&lt;this_thread::get_id()&lt;&lt;&quot;\t&quot;&lt;&lt;i&lt;&lt;&quot;\n&quot;;<br>
          ��������������������� sum+=i;<br>
          ����������� }<br>
          }<br>
          <br>
          int main()<br>
          {<br>
          ��� //boost::posix_time::ptime start =
          boost::posix_time::microsec_clock::local_time();<br>
          <br>
          ��� thread thrd(IteratorFunc,1);<br>
          ��� thread thrd2(IteratorFunc,2);<br>
          �� <br>
          ��� cout&lt;&lt;sum;<br>
          ��� thrd.join();<br>
          ��� thrd2.join();<br>
          }</blockquote></div></blockquote></div></div></div></div></blockquote></div></div></blockquote></div>