Hello Dear Squeakers,
threads and co. are giving me headaches :-) I wanted to ask for advice... What do I use in Squeak to prevent more than one thread entering a region, but "releasing" the exclusivity in another block/callback? For example, let's suppose there is an hypothetical class "Lock" that does that. lock acquire. longOperation performWhenFinished: [ callback code ... lock release ]. ... rest of the program ... Is there something like that? Or do I have to simulate it with something else? (Semaphore?) I am aware or Monitor/Mutex critical:, but I am not sure I can use it in my case. Thanks in advance, Sebastian _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On 2012-11-02, at 18:42, Sebastian Nozzi <[hidden email]> wrote: > Hello Dear Squeakers, > > threads and co. are giving me headaches :-) I wanted to ask for advice... > > What do I use in Squeak to prevent more than one thread entering a region, but "releasing" the exclusivity in another block/callback? For example, let's suppose there is an hypothetical class "Lock" that does that. > > lock acquire. > longOperation performWhenFinished: [ callback code ... lock release ]. > ... > rest of the program > ... > > Is there something like that? Or do I have to simulate it with something else? (Semaphore?) You could use a Semaphore directly: semaphore := Semaphore forMutualExclusion. and then semaphore wait "acquire lock" ... semaphore signal "release lock" > I am aware or Monitor/Mutex critical:, but I am not sure I can use it in my case. Semaphore>>critical: simply does a wait and signal like above. But it ensures that if something goes wrong the semaphore is still signaled, so it's more easy to use. - Bert - _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Thanks!
Didn't know it was so simple... I was kind of intimidated by "wait". Thought, well, that it suspended the thread no matter what. Another question, out of curiosity: Is it possible to have one thread wait for 2 other threads to finish? Would it also be solved with Semaphores like this? s1 := Semaphore forMutualExclusion. s2 := Semaphore forMutualExclusion. [ s1 wait. do stuff .... s1 signal] fork. [ s2 wait. do stuff ..... s2 signal ] fork. s1 wait. s2 wait. "Here we are sure the two other processes finished, kind of..." Or is there a more "friendly" way? :-) Thanks again! Sebastian > You could use a Semaphore directly: > > semaphore := Semaphore forMutualExclusion. > > and then > > semaphore wait "acquire lock" > ... > semaphore signal "release lock" > >> I am aware or Monitor/Mutex critical:, but I am not sure I can use it in my case. > > > Semaphore>>critical: simply does a wait and signal like above. But it ensures that if something goes wrong the semaphore is still signaled, so it's more easy to use. > > - Bert - > > _______________________________________________ > Beginners mailing list > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On 2012-11-02, at 19:24, Sebastian Nozzi <[hidden email]> wrote:
> Thanks! > > Didn't know it was so simple... I was kind of intimidated by "wait". Thought, well, that it suspended the thread no matter what. > > Another question, out of curiosity: > > Is it possible to have one thread wait for 2 other threads to finish? Would it also be solved with Semaphores like this? > > s1 := Semaphore forMutualExclusion. > s2 := Semaphore forMutualExclusion. > [ s1 wait. do stuff .... s1 signal] fork. > [ s2 wait. do stuff ..... s2 signal ] fork. > > s1 wait. > s2 wait. > "Here we are sure the two other processes finished, kind of..." > > Or is there a more "friendly" way? :-) > > Thanks again! > > Sebastian Looks okay to me. But you could make it a bit simpler: s1 := Semaphore new. s2 := Semaphore new. [ do stuff .... s1 signal] fork. [ do stuff ..... s2 signal] fork. s1 wait. s2 wait. Or even: s1 := Semaphore new. [ do stuff .... s1 signal] fork. [ do stuff ..... s1 signal] fork. s1 wait; wait. - Bert - > >> You could use a Semaphore directly: >> >> semaphore := Semaphore forMutualExclusion. >> >> and then >> >> semaphore wait "acquire lock" >> ... >> semaphore signal "release lock" >> >>> I am aware or Monitor/Mutex critical:, but I am not sure I can use it in my case. >> >> >> Semaphore>>critical: simply does a wait and signal like above. But it ensures that if something goes wrong the semaphore is still signaled, so it's more easy to use. >> >> - Bert - _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Hey, cool, thanks a lot!
El 02/11/2012, a las 19:40, Bert Freudenberg escribió: > On 2012-11-02, at 19:24, Sebastian Nozzi <[hidden email]> wrote: > >> Is it possible to have one thread wait for 2 other threads to finish? Would it also be solved with Semaphores like this? >> >> s1 := Semaphore forMutualExclusion. >> s2 := Semaphore forMutualExclusion. >> [ s1 wait. do stuff .... s1 signal] fork. >> [ s2 wait. do stuff ..... s2 signal ] fork. >> >> s1 wait. >> s2 wait. >> "Here we are sure the two other processes finished, kind of..." >> >> Or is there a more "friendly" way? :-) > > Looks okay to me. But you could make it a bit simpler: > > s1 := Semaphore new. > s2 := Semaphore new. > [ do stuff .... s1 signal] fork. > [ do stuff ..... s2 signal] fork. > s1 wait. > s2 wait. > > Or even: > > s1 := Semaphore new. > [ do stuff .... s1 signal] fork. > [ do stuff ..... s1 signal] fork. > s1 wait; wait. > > - Bert - Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Bert Freudenberg
On Fri, Nov 02, 2012 at 07:40:08PM +0100, Bert Freudenberg wrote:
> On 2012-11-02, at 19:24, Sebastian Nozzi <[hidden email]> wrote: > > > Thanks! > > > > Didn't know it was so simple... I was kind of intimidated by "wait". Thought, well, that it suspended the thread no matter what. > > > > Another question, out of curiosity: > > > > Is it possible to have one thread wait for 2 other threads to finish? Would it also be solved with Semaphores like this? > > > > s1 := Semaphore forMutualExclusion. > > s2 := Semaphore forMutualExclusion. > > [ s1 wait. do stuff .... s1 signal] fork. > > [ s2 wait. do stuff ..... s2 signal ] fork. > > > > s1 wait. > > s2 wait. > > "Here we are sure the two other processes finished, kind of..." > > > > Or is there a more "friendly" way? :-) > > > > Thanks again! > > > > Sebastian > > Looks okay to me. But you could make it a bit simpler: > > s1 := Semaphore new. > s2 := Semaphore new. > [ do stuff .... s1 signal] fork. > [ do stuff ..... s2 signal] fork. > s1 wait. > s2 wait. > > Or even: > > s1 := Semaphore new. > [ do stuff .... s1 signal] fork. > [ do stuff ..... s1 signal] fork. > s1 wait; wait. > And you can see how this works by writing a few things to the transcript. Try evaluating this in a workspace: Transcript clear. sema := Semaphore new. arrayOfFourProcesses := { [(Delay forSeconds: 8) wait. sema signal] fork. [(Delay forSeconds: 2) wait. sema signal] fork. [(Delay forSeconds: 6) wait. sema signal] fork. [(Delay forSeconds: 4) wait. sema signal] fork }. (Delay forMilliseconds: 100) wait. "allow the processes to start" Transcript cr; show: DateAndTime now asString. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString]. sema wait. Transcript cr; cr; show: DateAndTime now asString, ' one process just finished'. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString]. sema wait. Transcript cr; cr; show: DateAndTime now asString, ' another process just finished'. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString]. sema wait. Transcript cr; cr; show: DateAndTime now asString, ' and another process just finished'. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString]. sema wait. Transcript cr; cr; show: DateAndTime now asString, ' the last process just finished'. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString]. Dave _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Another great example. Thank you very much!
I had to evaluate the whole thing inside of a [ ... ] fork, in order to see the updates in the Transcript...
2012/11/2 David T. Lewis <[hidden email]>
_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |