Created
February 2, 2023 06:09
-
-
Save smigniot/adec2f97e6803ee3ed2d9867fb61cfd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import System.IO | |
import Control.Applicative | |
import Database.SQLite.Simple | |
import Database.SQLite.Simple.FromRow | |
import Control.Monad | |
import Control.Concurrent | |
main :: IO () | |
main = do | |
conn <- open "test.db" | |
execute_ conn "CREATE TABLE IF NOT EXISTS singleton (t TEXT)" | |
execute_ conn "DELETE FROM singleton" | |
execute_ conn "INSERT INTO singleton (t) VALUES ('blah')" | |
let total = 4000 | |
let nums = take 20 [2..] | |
let jobs = map (\n -> replicateM_ (div total n) $ foobar n conn) nums | |
sequence $ map forkIO jobs | |
forkIO $ replicateM_ (div total 12) $ showDb conn | |
milliSleep (5 * total) | |
putStrLn "" | |
close conn | |
foobar n conn = do | |
execute conn "UPDATE singleton SET t = ?" [("fork #" ++ show n) :: String] | |
milliSleep n | |
showDb conn = do | |
r <- query_ conn "SELECT t FROM singleton" | |
let s = (head $ head r) :: String | |
putStr s | |
putStr "\t" | |
hFlush stdout | |
milliSleep 11 | |
milliSleep = threadDelay . (*) 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment