On 05/20/2014 12:50 AM, user wrote:
 > Steve,
 > I'm remailing this because it didn't show up in my U-group list:

Hi Bob,

I may now be repeating what you've already learned, but just in case...

...
 > On Monday 19 May 2014 17:01:35 Steve Wampler wrote:
 >> What are the symptoms you're seeing?  Did you make L2 thread-safe?
 > Oops! How do I do that?

You can wrap it in a mutex.  E.g.:

     L2 := mutex(L2)

or

     L2 := mutex([])

...
 >> I have trouble understanding this.  Can you give a short, concrete,
 >> example?
 >
 > #----------------------------------------------------
 > procedure main()
 > T:=[];R:=[]
 > every (1 to 10) do put(T,thread put(R,gen()))  #CONSTANT no.of results!
 > #every (              put(T,thread put(R,gen()))) #SINGLE !
 > write("Number of Threads: ",*T)
 > write("Number of Results:  ",*R)
 > n:=0
 > every wait(!T) do write("Thread no.",n +:=1,"\nResult is ",R[n])
 > write("Threads finished!")
 > end
 > #----------------------------------------------------
 > procedure gen()
 > delay(2000)      ### to check (with top e.g.) if multi-threading does occur!
 > suspend (1 to 10) #CONSTANT no.of results, contrary to my actual code!
 > end
 > #----------------------------------------------------

Thanks.  You should wrap R into a mutex to make it thread safe.

I think things work, otherwise.  Except that all of the gen() produce
their values so quickly that you're unlikely to see any sort of randomness.
(most of the time you'll see "1 2 3 4 5 6 7 8 9 10" sequentially repeated
10 times...because they all delay the same amount of time before running
as quickly as they can through their result sequences.)

To verify that the threading is actually taking place, try changing gen()
to:

procedure gen()
     suspend (1 to 10) do delay(?200)
end

Which introduces a random delay into each resumption of gen().

I ran the following variant (the two changes suggested above, plus
showing the final values in R):
---------------------------------------------------------------------
procedure main()
     T:=[];R:=mutex([])
     every (1 to 10) do put(T,thread put(R,gen()))  #CONSTANT no.of results!
     write("Number of Threads: ",*T)
     write("Number of Results:  ",*R)
     n:=0
     every wait(!T) do write("Thread no.",n +:=1,"\nResult is ",R[n])
     write("Threads finished!")
     every writes(!R," ")
     write()
end

procedure gen()
     suspend (1 to 10) do delay(?200)
end
-------------------------------------------------------------------

And get the output:
-------------------------------------------------------------------
Number of Threads: 10
Number of Results:  0
Thread no.1
Result is 1
Thread no.2
Result is 1
Thread no.3
Result is 1
Thread no.4
Result is 1
Thread no.5
Result is 1
Thread no.6
Result is 1
Thread no.7
Result is 1
Thread no.8
Result is 1
Thread no.9
Result is 1
Thread no.10
Result is 1
Threads finished!
1 1 1 1 1 1 1 1 1 1 2 2 2 3 2 2 3 2 2 3 2 3 4 3 4 3 2 2 4 3 4 5 4 5 4 5 5 3 4 6 
5 3 5 4 6 3 5 6 5 6 6 6 7 7 7 6 6 8 4 4 
9 7 8 8 5 7 7 7 7 9 8 8 9 10 5 9 8 8 6 10 6 9 10 8 9 7 10 9 10 7 9 10 10 10 8 8 
9 10 9 10
------------------------------------------------------------

Is that what you'd like to see?  (Note that all of the threads generate their 
first value before any of them
can produce their second - the delay of 200ms is enough for all of them to get 
a time slice.)

 >>> The obvious solution would be to lose the "(GEN) do" but then only ONE
 >>> THREAD GETS CREATED !
 >>
 >> Yep - that's expected.
 > Can you explain please?

I think Jafar gave a better explanation than I can but it's because there are no
generators in:

     every put(T,thread put(R,gen()))

prior to the evaluation of 'thread', so no backtracking can go back to produce a
new thread.  [There's actually no generators at all that can affect the
evaluation of the every since the put(R,gen()) expression is 'bound' within
the thread and only evaluated within the execution of that thread.]

You would need to introduce a generator that produces successive
evaluations of 'thread put(R,gen())'.  One such way would be with:

     every put(T, |(thread put(R,gen())) \ 10)

which combines repeated alternation with limitation to produce a result
sequence whose results are 10 successive evaluations of 'thread put(R,gen())'
[There are other ways to get the same effect.]  Note that the evaluation
of 'thread put(R,gen())' produces a thread - the evaluation of the
*contents* of that thread are evaluated independently.

-Steve
-- 
Steve Wampler -- [email protected]
The gods that smiled on your birth are now laughing out loud.

------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to