pSpaces / goSpace

Programming with Spaces in Go
MIT License
10 stars 5 forks source link

Crash in simple producer/consumer program #14

Open albertolluch opened 6 years ago

albertolluch commented 6 years ago

The program below crashes:

// This is a simple producer/consumer system.

package main

import (
    "fmt"
    "math/rand"
    "time"

    . "github.com/pspaces/gospace"
)

// PRODUCERS defines the number of producers.
const PRODUCERS = 2

// NTASKS defines the number of tasks each producer generates.
const NTASKS = 2

// WORKERS defines the number of workers.
const WORKERS = 2

func main() {

    rand.Seed(time.Now().UTC().UnixNano())

    bag := NewSpace("bag")

    for i := 0; i < PRODUCERS; i++ {
        go producer(&bag, i, NTASKS)
    }

    for i := 0; i < WORKERS; i++ {
        go worker(&bag, i)
    }

    bag.Query("done")

}

func producer(bag *Space, me int, ntasks int) {
    for i := 0; i < ntasks; i++ {
        x := rand.Intn(10)
        bag.Put(x)
        fmt.Printf("Producer %d added %d to the bag...\n", me, x)
    }
}

func worker(bag *Space, me int) {
    var x int
    var y int
    for {
        // Get one number.
        bag.Get(&x)
        // Try to get another number.
        _, err := bag.GetP(&y)
        if err == nil {
            // If found then reduce.
            fmt.Printf("Worker %d got pair (%d,%d) to reduce.\n", me, x, y)
            x = x + y
            bag.Put(x)
            fmt.Printf("Worker %d added %d to the bag.\n", me, x)
        } else {
            // If not found, replace the first number.
            bag.Put(x)
        }
    }
}
ghost commented 6 years ago

This is now fixed in commit 3258710a4d0bcad58902e8a32aa222f7e62b61e7 in the aggregation-policy branch.

albertolluch commented 6 years ago

Is the branch stable or should I rather wait until the merge? This issue is not critical right now, so I can wait.

albertolluch commented 6 years ago

I am reopening this. There is still a "concurrent map write" crash on my side.