Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Metered-concurrency/Haskell/metered-concurrency-1.hs
Normal file
29
Task/Metered-concurrency/Haskell/metered-concurrency-1.hs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import Control.Concurrent
|
||||
( newQSem,
|
||||
signalQSem,
|
||||
waitQSem,
|
||||
threadDelay,
|
||||
forkIO,
|
||||
newEmptyMVar,
|
||||
putMVar,
|
||||
takeMVar,
|
||||
QSem,
|
||||
MVar )
|
||||
import Control.Monad ( replicateM_ )
|
||||
|
||||
worker :: QSem -> MVar String -> Int -> IO ()
|
||||
worker q m n = do
|
||||
waitQSem q
|
||||
putMVar m $ "Worker " <> show n <> " has acquired the lock."
|
||||
threadDelay 2000000 -- microseconds!
|
||||
signalQSem q
|
||||
putMVar m $ "Worker " <> show n <> " has released the lock."
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
q <- newQSem 3
|
||||
m <- newEmptyMVar
|
||||
let workers = 5
|
||||
prints = 2 * workers
|
||||
mapM_ (forkIO . worker q m) [1 .. workers]
|
||||
replicateM_ prints $ takeMVar m >>= putStrLn
|
||||
17
Task/Metered-concurrency/Haskell/metered-concurrency-2.hs
Normal file
17
Task/Metered-concurrency/Haskell/metered-concurrency-2.hs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
procedure main(A)
|
||||
n := integer(A[1] | 3) # Max. number of active tasks
|
||||
m := integer(A[2] | 2) # Number of visits by each task
|
||||
k := integer(A[3] | 5) # Number of tasks
|
||||
sem := [: |mutex([])\n :]
|
||||
every put(threads := [], (i := 1 to k, thread
|
||||
every 1 to m do {
|
||||
write("unit ",i," ready")
|
||||
until flag := trylock(!sem)
|
||||
write("unit ",i," running")
|
||||
delay(2000)
|
||||
write("unit ",i," done")
|
||||
unlock(flag)
|
||||
}))
|
||||
|
||||
every wait(!threads)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue