🧵 thread - Çok İş Parçacıklı Programlama

Thread, Mutex, Channel, Arc, Atomic, ThreadPool

67
function
878
lines
27 KB
Boyut

🚀 Quick Start

import thread

// Thread spawn
let handle = thread.spawn(function() do
    yazdır("Thread'den merhaba!")
    return 42
end)

let Result = thread.join(handle)
yazdır("Result: " + Result.yazıya())

// Mutex (mutual exclusion)
let sayaç = thread.mutex_new(0)

let threads = []
each i in aralık(0, 10) for do
    threads.ekle(thread.spawn(function() do
        each j in aralık(0, 100) for do
            thread.mutex_lock(sayaç, function(değer) do
                return değer + 1
            end)
        end
    end))
end

each t in threads for do
    thread.join(t)
end

let toplam = thread.mutex_get(sayaç)
yazdır("Toplam: " + toplam.yazıya())  // 1000

// Channel (message passing)
let (tx, rx) = thread.channel_create()

thread.spawn(function() do
    thread.channel_send(tx, "Mesaj 1")
    thread.channel_send(tx, "Mesaj 2")
end)

yazdır(thread.channel_receive(rx))  // "Mesaj 1"
yazdır(thread.channel_receive(rx))  // "Mesaj 2"

📚 Senkronizasyon

💡 Exampleler

Producer-Consumer Pattern

import thread, collections

let (tx, rx) = thread.channel_create()

// Producers
each i in aralık(0, 3) for do
    let sender = tx.clone()
    thread.spawn(function() do
        each j in aralık(0, 5) for do
            let mesaj = "Producer " + i.yazıya() + " - Item " + j.yazıya()
            thread.channel_send(sender, mesaj)
            thread.sleep(100)  // 100ms
        end
    end)
end

// Consumer
let consumer = thread.spawn(function() do
    let items = []
    
    loop do
        let Result = thread.channel_receive_timeout(rx, 1000)
        
        if Result.Ok_mı()  do
            let mesaj = Result.çıkar()
            yazdır("Tüketildi: " + mesaj)
            items.ekle(mesaj)
        end else do
            // Timeout - tüm producer'lar bitmiş
            kır
        end
    end
    
    return items.uzunluk()
end)

let toplam = thread.join(consumer)
yazdır("Toplam item: " + toplam.yazıya())  // 15

Parallel Map-Reduce

import thread

function parallel_map(veri: Dizi, f: function, thread_sayısı: tamsayı) -> Dizi do
    let chunk_boyutu = (veri.uzunluk() + thread_sayısı - 1) / thread_sayısı
    let handles = []
    
    each i in aralık(0, thread_sayısı) for do
        let başlangıç = i * chunk_boyutu
        let bitiş = math.min(başlangıç + chunk_boyutu, veri.uzunluk())
        
        if başlangıç >= veri.uzunluk()  do
            devam
        end
        
        let chunk = veri.dilimle(başlangıç, bitiş)
        
        let handle = thread.spawn(function() do
            return chunk.map(f)
        end)
        
        handles.ekle(handle)
    end
    
    // Resultları birleştir
    let Result = []
    each handle in handles for do
        let chunk_result = thread.join(handle)
        Result = Result + chunk_result
    end
    
    return Result
end

function parallel_reduce(veri: Dizi, f: function, başlangıç: herhangi) -> herhangi do
    let partial_results = parallel_map(veri, function(x) do return x end, 4)
    
    // Local reduce'lar
    let handles = []
    let chunk_boyutu = (partial_results.uzunluk() + 4 - 1) / 4
    
    each i in aralık(0, 4) for do
        let başlangıç_idx = i * chunk_boyutu
        let bitiş_idx = math.min(başlangıç_idx + chunk_boyutu, partial_results.uzunluk())
        
        if başlangıç_idx >= partial_results.uzunluk()  do
            devam
        end
        
        let chunk = partial_results.dilimle(başlangıç_idx, bitiş_idx)
        
        handles.ekle(thread.spawn(function() do
            let acc = başlangıç
            each x in chunk for do
                acc = f(acc, x)
            end
            return acc
        end))
    end
    
    // Final reduce
    let Result = başlangıç
    each handle in handles for do
        Result = f(Result, thread.join(handle))
    end
    
    return Result
end

// useım: 1'den 1_000_000'a kadar topla
let sayılar = aralık(1, 1_000_001).collect()
let toplam = parallel_reduce(sayılar, function(a, b) do return a + b end, 0)
yazdır("Toplam: " + toplam.yazıya())  // 500000500000

← All Modules