The (10$0 1) # was to work around the value error -- it's debugging code.

However, I get the crash after removing it entirely. This happens
using f. and this happens with task defined as a global name:

metcon=: {{
  lock=: 10 T. 0
  sleep=: 6!:3
  task=: {{
    echo 'Task ',y,&":' waiting...'
    11 T. lock              NB. wait
    sleep 2
    echo r=.'Task ',y,&":' has the semaphore'
    13 T. lock              NB. release
    r rplc 'has';'had'
  }}
  0&T.@'' each i.0>.4-1 T.'' NB. ensure at least four threads
  echo (":1 T. ''),' threads'
  r=. > task t.''"0 i.10    NB. dispatch and wait for 10 tasks
  14 T. lock                NB. discard lock
  r
}}

Thanks,

-- 
Raul


On Wed, Jun 1, 2022 at 7:19 PM Elijah Stone <[email protected]> wrote:
>
> Right...  I was chasing the wrong issue.  The code is wrong.
>
> The issue is that 'task' is a proverb, and the new thread does not inherit
> metcon's lexical scope (which would be tantamount to closures...maybe not a
> bad idea...), so 'task' is looked up globally, and found nowhere.  Fixes
> include making 'task' global (like 'lock' and 'sleep'), or using f. as I
> suggested before.
>
> That the code appeared to work when not creating any threads is a bug.
>
> Incidentally, there is another issue: you destroy the mutex before ensuring
> that all threads are done.  I suggest transposing (10$0 1)&# and > .
>
> On Wed, 1 Jun 2022, Elijah Stone wrote:
>
> > BTW, 11 T.lock is equivalent to 11 T.lock;_ .
> >
> > On Wed, 1 Jun 2022, Raul Miller wrote:
> >
> >> Oops, bad copy and paste
> >>
> >> Ignore the first (incomplete) declaration of metcon.
> >>
> >> Actually, please treat the declaration at
> >> http://www.jsoftware.com/pipermail/beta/2022-June/010566.html as
> >> canonical for this issue.
> >>
> >> Thanks,
> >>
> >> --
> >> Raul
> >>
> >> On Wed, Jun 1, 2022 at 3:57 PM Raul Miller <[email protected]> wrote:
> >>>
> >>> https://www.rosettacode.org/wiki/Metered_concurrency
> >>>
> >>> I wanted to put together a more serious J implementation of the
> >>> rosettacode metered concurrency task.
> >>>
> >>> Unfortunately, either (a) I do not understand how the threading
> >>> primitives are supposed to work, or (b) they are not working properly.
> >>>
> >>> metcon=: {{
> >>>   lock=: 10 T. 0
> >>>   sleep=: 6!:3
> >>> metcon=: {{
> >>>   lock=: 10 T. 0
> >>>   sleep=: 6!:3
> >>>   task=. {{
> >>>     11 T. lock;_            NB. wait
> >>>     sleep 2
> >>>     echo r=.'Task ',y,&":' has the semaphore'
> >>>     13 T. lock              NB. release
> >>>     r rplc 'has';'had'
> >>>   }}
> >>>   0&T.@'' each i.0>4-1 T.'' NB. ensure at least four threads
> >>>   r=. task t.''"0 i.10      NB. dispatch 10 tasks
> >>>   #each r                   NB. wait for completion
> >>>   14 T. lock                NB. discard lock
> >>>   r
> >>> }}
> >>> I expect this to take about 20 seconds (because of the use of 6!:3)
> >>> and run ten tasks in 3 of the four guaranteed threads.
> >>>
> >>> Instead, what I get is this:
> >>>
> >>>    metcon''
> >>> Task 1 has the semaphore
> >>> Task 3 has the semaphore
> >>> Task 5 has the semaphore
> >>> Task 7 has the semaphore
> >>> Task 9 has the semaphore
> >>> |value error
> >>> |       #each r
> >>>
> >>> So it looks like half of my tasks are getting lost somewhere, and it
> >>> looks like all of the results from those tasks are getting lost
> >>> somewhere.
> >>>
> >>>    JVERSION
> >>> Engine: j904/j64avx2/windows
> >>> Beta-d: commercial/2022-05-19T20:39:35
> >>> Library: 9.04.01
> >>> Qt IDE: 2.0.3/6.2.4(6.2.4)
> >>> Platform: Win 64
> >>> Installer: J904 install
> >>> InstallPath: c:/other/j904
> >>> Contact: www.jsoftware.com
> >>>
> >>> Thanks,
> >>>
> >>> --
> >>> Raul
> >> ----------------------------------------------------------------------
> >> For information about J forums see http://www.jsoftware.com/forums.htm
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to