2012-12-31 12 views
6

कहें कि मैं हास्केल में दो अलग-अलग धागे में दो लंबी चल रही प्रक्रियाओं की गणना करना चाहता हूं। हालांकि, मैं केवल पहले किए गए परिणाम से परिणाम की परवाह करता हूं। यह मैं कैसे करूंगा?पहले धागे से परिणाम प्राप्त करें

उदाहरण (छद्म कोड): http://hackage.haskell.org/package/unamb

नहीं तो आप unamb के रूप में एक ही बात कर सकता है लेकिन killThread बाहर छोड़:

thread1 = spark $ long_running some_arg1 
thread2 = spark $ long_running some_arg2 
result = first_done thread1 thread2 -- Maybe even first_done [thread1, thread2]? 

उत्तर

17

async पैकेज यह करता है, और अब Haskell Platform का हिस्सा है।

import Control.Concurrent.Async 
import Control.Concurrent (threadDelay) 

main :: IO() 
main = do 
    x <- async (threadDelay 2000000 >> return 1) 
    y <- async (threadDelay 1000000 >> return 2) 
    (_, res) <- waitAnyCancel [x, y] 
    print (res :: Int) 
+0

यह वही है जो मैं ढूंढ रहा था, धन्यवाद! – bheklilr

3

आप लंबे समय तक चल रहे धागा मार डाला आप उपयोग कर सकते हैं।

2

आप IO धागे के साथ काम करते हैं, तो आप भी नवीनतम parallel-io पैकेज इस्तेमाल कर सकते हैं, विशेष रूप से parallelFirst में।

संबंधित मुद्दे