Control.Concurrent

Prelude> import Control.Concurrent
Prelude Control.Concurrent>

Control.Concurrent 模块属于标准库,不需要安装。

forkIO threadDelay

  • data ThreadId

    代表线程句柄的类型。
  • forkIO :: IO () -> IO ThreadId

    创建线程返回线程句柄,在新的线程中运行指定的 IO 操作。
  • threadDelay :: Int -> IO ()

    当前线程挂起 n 微秒(百万分之一秒,10负6次方分之一秒)。

示例 1

haskell-simple-concurrency

module Threads where

import Control.Concurrent (forkIO, threadDelay)
import Data.Foldable (for_) main = do
-- Synchronously perform some work.
printMessagesFrom "main" -- Fork a new thread to do some work in the background.
forkIO $ printMessagesFrom "fork" -- Fork another thread using an inline function!
forkIO $ do
putStrLn "starting!"
sleepMs 5
putStrLn "ending!" -- Wait for threads to finish.
sleepMs 10 -- A simple function that prints three messages with a little delay between them.
printMessagesFrom name = for_ [1..3] printMessage
where printMessage i = do
sleepMs 1
putStrLn (name ++ " number " ++ show i) -- A utility function - threadDelay takes microseconds, which is slightly annoying.
sleepMs n = threadDelay (n * 1000)
*Threads> main
main number 1
main number 2
main number 3
starting!
fork number 1
fork number 2
fork number 3
ending!
  • forkIO $ printMessagesFrom "fork"

    forkIO $ do putStrLn "starting!"; sleepMs 5; putStrLn "ending!"

    这里使用 forkIO 先后启动了两个线程:打印数字的线程和打印开始结束的线程

    打印开始结束的过程为:打印开始,然后等待 5 毫秒,最后打印结束。
  • printMessagesFrom name = for_ [1..3] printMessage

    打印数字函数一共打印三个数字
  • printMessage i = do sleepMs 1; putStrLn (name ++ " number " ++ show i)

    每次打印数字之前先等待 1 毫秒
  • 所以最后结果为
starting!          -- 0 毫秒
fork number 1 -- 1 毫秒
fork number 2 -- 2 毫秒
fork number 3 -- 3 毫秒
ending! -- 5 毫秒

STM

$ cabal install stm
stm-2.4.5.0 installed

STM(Software Transactional Memory,软件事务内存),是由软件实现的一种用于控制线程间同步的机制。

Haskell语言中,STM 是个 Monad。

  • atomically :: STM a -> IO a

    在 IO Monad 中作为原子操作来执行 STM Action。

TMVar

  • data TMVar a

    TMVar 是同步的变量,用于线程间的同步。TMVar 变量可以想象成一个只能放一个东西的盒子,状态可以为满也可以为空。
  • newTMVar :: a -> STM (TMVar a)

    创建初期状态为满的 TMVar 变量,内容为指定的值。
  • newEmptyTMVar :: STM (TMVar a)

    创建初期状态为空的 TMVar 变量。
  • takeTMVar :: TMVar a -> STM a

    取出 TMVar 变量中的值,状态由满变空。如果TMVar 变量中没有值状态为空,就一直等待(阻塞)并不断重新尝试。
  • putTMVar :: TMVar a -> a -> STM ()

    将指定的值放入 TMVar 变量中,状态由空变满。如果TMVar 变量中已经有值状态为满,就一直等待(阻塞)并不断重新尝试。
  • readTMVar :: TMVar a -> STM a

    takeTMVar 和 putTMVar 的结合体,先从 TMVar 变量中取出值,然后再放回,最后返回这个值。

示例 2

module TMVars where

import Threads (sleepMs)

import Control.Concurrent.STM (atomically)
import Control.Concurrent (forkIO)
import Control.Concurrent.STM.TMVar (newEmptyTMVar, takeTMVar, putTMVar) main = do
result <- atomically $ newEmptyTMVar forkIO $ do
-- Pretend there is some actual work to do.
sleepMs 5
putStrLn "Calculated result!"
atomically $ putTMVar result 42 putStrLn "Waiting..."
value <- atomically $ takeTMVar result
putStrLn ("The answer is: " ++ show value)
*TMVars> main
Waiting...
Calculated result!
The answer is: 42
  • result <- atomically newEmptyTMVar

    主线程创建空的 TMVar 变量 result。
  • value <- atomically $ takeTMVar result

    主线程不断尝试从 result 这个 TMVar 变量中取值。
  • forkIO (do sleepMs 5; putStrLn "Calculated result!"; atomically $ putTMVar result 42)

    forkIO 启动一个新的线程,先挂起 5 毫秒,然后将 42 放入 result 这个 TMVar 变量当中。

示例 3

module TMVarSharedState where

import Threads (sleepMs)

import Control.Concurrent (forkIO)
import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TMVar (newTMVar, takeTMVar, putTMVar)
import Control.Monad (replicateM) main = do
counter <- atomically $ newTMVar 0 let increment = do
count <- atomically $ takeTMVar counter
atomically $ putTMVar counter $! count + 1
incrementer = do
replicateM 1000 increment
return () threads <- replicateM 5 (forkIO incrementer) sleepMs 100
count <- atomically $ takeTMVar counter
print count
*TMVarSharedState> main
5000
  • counter <- atomically $ newTMVar 0

    主线程创建一个值为 0 的 TMVar 变量 counter。
  • ncrement = do count <- atomically $ takeTMVar counter; atomically $ putTMVar counter $! count + 1

    increment 函数将 TMVar 变量 counter 中的值加一(取出,加一,放回)。
  • incrementer = do replicateM 1000 increment; return ()

    incrementer 函数执行 1000 次 increment,即 1000 次“取出,加一,放回”。
  • replicateM 5 (forkIO incrementer)

    启动 5 个线程各自执行一次 incrementer 函数,总共 5000 次“取出,加一,放回”。
  • sleepMs 100; count <- atomically $ takeTMVar counter

    主线程等待 0.1 秒之后尝试取值
  • 5000

    结果正确,5 个线程之间没有发生数据竞争(data races)。

TChan

  • data TChan a

    TChan 是一种没有边界的先进先出(FIFO)的 channel 类型。
  • newTChan :: STM (TChan a)

    新建一个 TChan。
  • readTChan :: TChan a -> STM a

    从 TChan 中读出下一个值。
  • writeTChan :: TChan a -> a -> STM ()

    向 TChan 中写入一个值。
  • dupTChan :: TChan a -> STM (TChan a)

    复制一个 TChan,在此之后任何向源 TChan 写入的数据将被复制到作为复制品的那个 TChan,反之亦然。

示例 4

module TChan1 where

import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TChan (newTChan, writeTChan, readTChan) main = do
messages <- atomically newTChan
atomically $ writeTChan messages "unbounded"
atomically $ writeTChan messages "channels" -- Read a message from the channel, then output it.
msg <- atomically $ readTChan messages
putStrLn msg
-- Do the same thing again, but more concisely.
putStrLn =<< (atomically $ readTChan messages)
*TChan1> main
unbounded
channels

程序先创建一个 TChan,向其中写入两个字符串,然后再读出。

示例 5

module TChan2 where

import Control.Monad.STM
import Control.Concurrent
import Control.Concurrent.STM.TChan oneSecond = 1000000 writerThread :: TChan Int -> IO ()
writerThread chan = do
atomically $ writeTChan chan 1
threadDelay oneSecond
atomically $ writeTChan chan 2
threadDelay oneSecond
atomically $ writeTChan chan 3
threadDelay oneSecond readerThread :: TChan Int -> IO ()
readerThread chan = do
newInt <- atomically $ readTChan chan
putStrLn $ "read new value: " ++ show newInt
readerThread chan main = do
chan <- atomically $ newTChan
forkIO $ readerThread chan
forkIO $ writerThread chan
threadDelay $ 5 * oneSecond
*TChan2> main
read new value: 1
read new value: 2
read new value: 3
  • writerThread 线程每隔一秒向 chan 里面写入一个数。
  • readerThread 线程不断地尝试从 chan 里读出数并打印。

示例 6

module DuplicatingTChan where

import Threads (sleepMs)

import Control.Concurrent.STM
import Control.Concurrent (forkIO)
import Control.Concurrent.STM.TChan (newTChan, writeTChan, readTChan, dupTChan) nonDuplicatedTest = do
messages <- atomically newTChan
forkIO $ messageReader messages "First"
forkIO $ messageReader messages "Second"
atomically $ writeTChan messages "Hi!" messageReader channel name = do
msg <- atomically $ readTChan channel
putStrLn (name ++ " read: " ++ msg) duplicatedTest = do
broadcast <- atomically newTChan
forkIO $ broadcastReader broadcast "Third"
forkIO $ broadcastReader broadcast "Fourth"
sleepMs 1
atomically $ writeTChan broadcast "Bye!" broadcastReader channel name = do
channel' <- atomically $ dupTChan channel
messageReader channel' name main = do
nonDuplicatedTest
duplicatedTest
sleepMs 10
*DuplicatingTChan> main
First read: Hi!
Third read: Bye!
Fourth read: Bye!
  • 对于一个 channel A,写入 A 的信息只能通过 A 读出一次。
  • 调用 dupTChan 函数将 channel A 复制为 channel B 时,写入 A 的信息可以通过 B 再次读出。

TVar

  • data TVar a

    支持 STM 的可共享的内存变量。
  • newTVar :: a -> STM (TVar a)

    新建一个 TVar,值为 a。
  • readTVar :: TVar a -> STM a

    读取 TVar 的值
  • writeTVar :: TVar a -> a -> STM ()

    将值写入 TVar
  • modifyTVar :: TVar a -> (a -> a) -> STM ()

    修改 TVar 的值

示例 7

module TVarSharedState where

import Threads (sleepMs)

import Control.Concurrent (forkIO)
import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TVar (newTVar, readTVar, writeTVar)
import Control.Monad (replicateM) main = do
counter <- atomically $ newTVar 0 let increment = atomically $ do
count <- readTVar counter
writeTVar counter $! count + 1
incrementer = do
replicateM 1000 increment
return () threads <- replicateM 5 (forkIO incrementer) sleepMs 100
count <- atomically $ readTVar counter
print count
*TVarSharedState> main
5000
  • increment = atomically $ do count <- readTVar counter; writeTVar counter $! count + 1

    注意这里读写必须组合成原子操作,如果改成

    increment = do count <- atomically $ readTVar counter; atomically $ writeTVar counter $! count + 1

    的话,不同线程之间会发生数据竞争,即所有线程可能都会先读取同一个值,再写同一个值

    最终结果不定,可以是5000,也可以是1000。

TMVar 和 TVar

newtype TMVar a = TMVar (TVar (Maybe a))

从目前的实现上看,TMVar 封装的是 TVar (Maybe a),所谓状态为空为满是通过 Maybe 类型来实现的。

TMVar 和 MVar

MVar 是标准库所实现的的 IO Monad 中的同步变量,已不推荐使用。

与此相对应,TMVar 是 STM Monad 中的同步变量,更为健壮。

TArray

支持 STM 以及 MArray 接口的数组类型。

Haskell语言学习笔记(84)Concurrent的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(85)Async

    安装 async $ cabal install async async-2.2.1 installed async / wait / concurrently async :: IO a -> ...

  7. Haskell语言学习笔记(72)Free Monad

    安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

  8. Haskell语言学习笔记(49)ByteString Text

    Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ...

  9. Haskell语言学习笔记(44)Lens(2)

    自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...

随机推荐

  1. [UE4]场景截图

    要点: 一.鼠标在编辑器关卡中点一下,然后按F9键截图,右下角会出现截图后的文件链接,点击后会自动打开截图文件所在的文件夹.截图的大小正好的关卡视图的大小. 编辑器:F9键截图,只截图关卡视图界面 运 ...

  2. [UE4]条件语句Select

    select接收3个参数,输出一个值. 当条件为true时,返回输入到True节点的值. 当条件为false时,返回输入到false节点的值. select的输入和输出参数也可以是整数.float.V ...

  3. hue中使用oozie的workflow执行mr

    workflow创建任务 进入hue–Workflows–编辑器–workflow–创建 拖一个mapreduce作业(在页面靠近顶端有一排选项图标)到页面中间虚线框中 Jar路径必须是hdfs中ja ...

  4. spark使用hadoop native库

    默认情况下,hadoop官方发布的二进制包是不包含native库的,native库是用C++实现的,用于进行一些CPU密集型计算,如压缩.比如apache kylin在进行预计算时为了减少预计算的数据 ...

  5. Anatomy of a Program in Memory.剖析程序的内存布局

    原文标题:Anatomy of a Program in Memory 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩文章翻译 ...

  6. 「2017 山东一轮集训 Day6」子序列(矩阵快速幂)

    /* 找出了一个dp式子 是否能够倍增优化 我推的矩阵不太一样 是 1 0 0 0 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 2 求得逆矩阵大概就是 1 0 0 ...

  7. Web Service进阶

    选框架犹如选媳妇,选来选去,最后我还是选了“丑媳妇(CXF)”,为什么是它?因为 CXF 是 Apache 旗下的一款非常优秀的 WS 开源框架,具备轻量级的特性,而且能无缝整合到 Spring 中. ...

  8. uWSGI, Gunicorn, 啥玩意儿?

    因为nginx等优秀的开源项目,有不少本来不是做服务器的同学也可以写很多服务器端的程序了.但是在聊天中会发现,大家虽然写了不少代码,但是对wsgi是什么,gunicorn是什么,反向代理又是什么并不了 ...

  9. es6(12)--类,对象

    //类,对象 { //基本定义和生成实例 class Parent{ //定义构造函数 constructor(name='QQQ'){ this.name=name; } } let v_paren ...

  10. truncate(can)

    1. 如果给了参数, 从头截取到参数位置 2. 如果不给参数, 从头截取到光标 f = open("司马光砸缸", mode="r+", encoding=&q ...