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
On 2012-11-02, at 18:42, Sebastian Nozzi sebnozzi@gmail.com 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 -
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 Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
On 2012-11-02, at 19:24, Sebastian Nozzi sebnozzi@gmail.com 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 -
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 sebnozzi@gmail.com 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 -
On Fri, Nov 02, 2012 at 07:40:08PM +0100, Bert Freudenberg wrote:
On 2012-11-02, at 19:24, Sebastian Nozzi sebnozzi@gmail.com 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
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 lewis@mail.msen.com
On Fri, Nov 02, 2012 at 07:40:08PM +0100, Bert Freudenberg wrote:
On 2012-11-02, at 19:24, Sebastian Nozzi sebnozzi@gmail.com 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 Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
beginners@lists.squeakfoundation.org