<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 09/17/2014 04:51 PM, Xiangrong Fang
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAP93jB0aQEZF+PvEF2MP47qAy1Zz7HOMamqPhtWvFsMq1ZhBRw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div class="gmail_default" style="font-family:courier
          new,monospace"><span style="font-family:arial">2014-09-17
            16:09 GMT+08:00 Michael Schnell </span><span dir="ltr"
            style="font-family:arial"><<a moz-do-not-send="true"
              href="mailto:mschnell@lumino.de" target="_blank">mschnell@lumino.de</a>></span><span
            style="font-family:arial">:</span></div>
        <div class="gmail_extra">
          <div class="gmail_quote">
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              <div bgcolor="#FFFFFF" text="#000000"><br>
                Here you are. <br>
              </div>
            </blockquote>
            <div><br>
            </div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace">It is strange that it works on one
              computer but does not work (or partially work) on another
              -- causing SIGSEGV.  Both running same environment -
              Ubuntu 14.04 x86_64 fpc 2.6.4 laz1.2.4.</div>
          </div>
        </div>
      </div>
    </blockquote>
    That in fact is strange. <br>
    Of course here (Linux x86_32, newest svn version of fpc and
    Lazarus)  it does work, otherwise I would not have posted it. <br>
    <br>
    Did you try the unmodified project ? <br>
    <br>
    <blockquote
cite="mid:CAP93jB0aQEZF+PvEF2MP47qAy1Zz7HOMamqPhtWvFsMq1ZhBRw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div class="gmail_extra">
          <div class="gmail_quote">
            <div class="gmail_default" style="font-family:'courier
              new',monospace"><br>
            </div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace">I don't understand how the critical
              section works.   </div>
          </div>
        </div>
      </div>
    </blockquote>
    TCriticalSection is a straight forward semaphore. (->
    <a class="moz-txt-link-freetext" href="https://en.wikipedia.org/wiki/Semaphore_%28programming%29">https://en.wikipedia.org/wiki/Semaphore_%28programming%29</a> )<br>
    <br>
    Of course there are several kinds of semaphores out there: <br>
     - binary and counting: TCriticalSection is binary<br>
     - blocking or not blocking when a Thread/Task tries to take it
    while it already holds it: TCriticalSection does not block<br>
     - allowing or disallowing a Thread/Task to free it even though it
    is helt by another Thread/Task: TCriticalSection disallows this. <br>
    <br>
    <br>
    <blockquote
cite="mid:CAP93jB0aQEZF+PvEF2MP47qAy1Zz7HOMamqPhtWvFsMq1ZhBRw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div class="gmail_extra">
          <div class="gmail_quote">
            <div class="gmail_default" style="font-family:'courier
              new',monospace">I try to write a test program, which does
              the following:</div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace"><br>
            </div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace">The main thread create a predefined number
              of threads, which start running on creation. The thread's
              wait for a critical section variable, when acquired, try
              to crack a given integer, see if it is prime number or
              not, report back to main thread about the result and get
              the next number to crack from main thread.  The main
              thread output crack result of every thread in a memo box.</div>
          </div>
        </div>
      </div>
    </blockquote>
    Are you sure that only the main thread does anything related to the
    GUI ? This is strictly forbidden for worker threads and results in
    SIGSEGV. This is why TThreadPool offers events OnReady events that
    are run in the main thread. ("synchronized"). <br>
    <blockquote
cite="mid:CAP93jB0aQEZF+PvEF2MP47qAy1Zz7HOMamqPhtWvFsMq1ZhBRw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div class="gmail_extra">
          <div class="gmail_quote">
            <div class="gmail_default" style="font-family:'courier
              new',monospace"><br>
            </div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace">I simply don't know how to coordinate the
              threads. And got many strange errors like</div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace"><br>
            </div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace">Semaphore init failed (maybe too many
              running threads)?</div>
          </div>
        </div>
      </div>
    </blockquote>
    I never saw such an error. Anyway, I suppose you will do
    multithreading  just for sake of performance. Here running more
    threads than CPUs/Hardware Threads does not make sense, as it will
    slow down the calculation due to unnecessary time slice related task
    switches which result in local cache misses (Linux tries to keep a
    thread on the same CPU if possible, but this is only possible when
    there are not much more active threads than CPUs). So there will
    never be "too many" threads. <br>
    <br>
    In any case start with simple stuff (e.g. using my example and
    replacing the "Tasks" with first simple and then more complex 
    "Execute" procedures.<br>
    <br>
    <blockquote
cite="mid:CAP93jB0aQEZF+PvEF2MP47qAy1Zz7HOMamqPhtWvFsMq1ZhBRw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div class="gmail_extra">
          <div class="gmail_quote"><br>
            <div class="gmail_default" style="font-family:'courier
              new',monospace">something wrong in raise.c, laz asked me
              if I want to locate myself, blahblah.</div>
          </div>
        </div>
      </div>
    </blockquote>
    I have no idea what "raise.c" is supposed to be. <br>
    <blockquote
cite="mid:CAP93jB0aQEZF+PvEF2MP47qAy1Zz7HOMamqPhtWvFsMq1ZhBRw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div class="gmail_extra">
          <div class="gmail_quote">
            <div class="gmail_default" style="font-family:'courier
              new',monospace"><br>
            </div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace"><br>
            </div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace">May I send you my source, or you can write
              a demo for me?  Thanks a lot!</div>
            <div class="gmail_default" style="font-family:'courier
              new',monospace"><br>
            </div>
            <br>
          </div>
        </div>
      </div>
    </blockquote>
    I already did provide you with a demo. I can enhance this for you if
    you have a certain request. <br>
    <br>
    -Michael<br>
  </body>
</html>