Further notes:

(1) the use of 'r' here is primarily for debugging purposes. it's not
actually needed for the rosettacode task, and I would hope I could
trim most of that away once I have it working properly.

(2) It looks like I am not actually creating threads.

If I use this version of the code:

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=. > (10$0 1) # task t.''"0 i.10    NB. dispatch and wait for 10 tasks
  14 T. lock                NB. discard lock
  r
}}

It runs like this:

   metcon''
1 threads
Task 1 waiting...
Task 1 has the semaphore
Task 3 waiting...
Task 3 has the semaphore
Task 5 waiting...
Task 5 has the semaphore
Task 7 waiting...
Task 7 has the semaphore
Task 9 waiting...
Task 9 has the semaphore
Task 1 had the semaphore
Task 3 had the semaphore
Task 5 had the semaphore
Task 7 had the semaphore
Task 9 had the semaphore

If I create a thread manually, it looks like fewer of my tasks actually run:

   0 T. ''
2
   metcon''
2 threads
Task 2 waiting...
Task 2 has the semaphore
Task 5 waiting...
Task 5 has the semaphore
|value error: metcon
|   r=.    >(10$0 1)#task t.''"0 i.10

Hopefully I don't need to say anything more about this issue?

Thanks,

-- 
Raul

On Wed, Jun 1, 2022 at 4:06 PM Raul Miller <[email protected]> wrote:
>
> After thinking about this for a minute or so, I realized I should have
> used this expression, instead:
>
>   r=. > task t.''"0 i.10    NB. dispatch and wait for 10 tasks
>
> That said, this still gives me a value error (and half the tasks still
> do not run, which appears to be the cause of the value error).
>
> If I replace the above with:
>
>   r=. > (10$0 1) # task t.''"0 i.10
>
> I do have results for five of my tasks.
>
> 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

Reply via email to