Home > Backend Development > Golang > A simple Go simulation - concurrency issues

A simple Go simulation - concurrency issues

PHPz
Release: 2024-02-09 14:10:10
forward
711 people have browsed it

A simple Go simulation - concurrency issues

php Editor Shinichi brings you a simple but interesting Go simulation game called "Concurrency Problem". This game takes concurrent programming as its theme, allowing players to experience the charm of concurrent programming in a virtual world. In the game, players need to write code to handle the simultaneous execution of multiple tasks and test their concurrent programming abilities. The game interface is concise and clear, and the operation is simple, suitable for beginners to get started, and it also provides multiple difficulty and challenge modes for players to choose from. Whether you are a beginner or an experienced developer, you can enjoy the fun of concurrent programming in this simulation game.

Question content

I am a student from Poland and this semester I started a concurrent programming course (Go, Ada and some theoretical and CSP languages ​​in the future). To be honest, Golang looks interesting but I'm a bit confused. The bottom line is that in my experience I would call myself a below average programmer. Basically, my task is to create a simulation, which I will describe like this:

  • There is an n*m grid

  • Travelers can be generated randomly, up to k travelers, each traveler has a unique ID (1, 2, 3, etc., up to k)

  • At a random moment, if the space is free (I'm sure the free space is 0), the traveler can move up, left, right or down on the grid

  • There is also a camera that sometimes prints the current state of the mesh as well as recent movements (not implemented yet)

  • Unofficially, I've heard that I should use channels, whatever that means

My idea is to create a structure with the id and coordinates of each traveler and send their id to a channel that represents willingness to move, and then I will randomly choose the direction of movement.

I'm a little confused about concurrency - not only if and where should I use wgs and mutexes, but also if for example if I do go func(){} should the loop be inside or outside. I would be very happy to appreciate any tips, help or fixes/ideas to fix my code because currently, as you guessed, it is not working properly (e.g. when the camera prints the grid, sometimes there are more than k travelers , where multiple travelers share the same number and sometimes they seem to disappear). Hope everyone has a great day and I'd really appreciate any help :)

package main;

import(
    "fmt"
    "os"
    "strconv"
    "math/rand"
    //"sync"
    "time"
)

type traveler struct{
    id int;
    x int;
    y int;
}

func main(){

    //command line
    n, err := strconv.Atoi(os.Args[1]);
    m, err := strconv.Atoi(os.Args[2]);
    k, err := strconv.Atoi(os.Args[3]);
    if err != nil{
        panic(err)
        return
    }

    //board
    var grid [][]int;
    grid = make([][]int, n)
    for i:=0; i<n; i++{
        grid[i] = make([]int, m)
    }

    //array of travelers, channel for moves and id
    travelers := make([]traveler, k)
    no_of_travelers := 0;
    move := make(chan int, k);
    id := 1;

    //var wg sync.WaitGroup

    go camera(grid);

    go func() {
        
        for i:=0; i<len(travelers); i++ {   
            if no_of_travelers<k{
                travelers[i] = spawnTraveler(&id,grid);
                no_of_travelers++;
            }
        }
    }()

    go func() {
        for{
            a:= rand.Intn(k);
            sendMoveTraveler(&travelers[a], move);
        }
    }()

    receiveMoveTraveler(travelers, move, grid);

}

func spawnTraveler(id *int, grid [][]int) traveler{
    x:=-1;
    y:=-1;
    for{
        x = rand.Intn(len(grid));
        y = rand.Intn(len(grid));
        if(grid[x][y]==0){
            break;
        }
    }
    t := traveler{id: *id, x: x, y:y};
    grid[x][y] = *id;
    *id++;
    return t;
}


func sendMoveTraveler(t *traveler, move chan int){
        move <- t.id
}

func receiveMoveTraveler(travelers []traveler, move chan int, grid [][]int){
    for{
        id := <- move
        for i:=0; i<len(travelers); i++{
            if travelers[i].id == id{
                direction := rand.Intn(4); //1-left 2-up 3-right 4-down
                switch direction {
                case 0:
                    if travelers[i].x>0 && grid[travelers[i].x-1][travelers[i].y] == 0{
                        grid[travelers[i].x-1][travelers[i].y] = grid[travelers[i].x][travelers[i].y];
                        grid[travelers[i].x][travelers[i].y] = 0;
                        travelers[i].x = travelers[i].x-1;
                        travelers[i].y = travelers[i].y;
                    }
                case 1:
                    if travelers[i].y>0 && grid[travelers[i].x][travelers[i].y-1] == 0{
                        grid[travelers[i].x][travelers[i].y-1] = grid[travelers[i].x][travelers[i].y];
                        grid[travelers[i].x][travelers[i].y] = 0;
                        travelers[i].x = travelers[i].x;
                        travelers[i].y = travelers[i].y-1;
                    }
                case 2:
                    if travelers[i].x<len(grid)-1 && grid[travelers[i].x+1][travelers[i].y] == 0{
                        grid[travelers[i].x+1][travelers[i].y] = grid[travelers[i].x][travelers[i].y];
                        grid[travelers[i].x][travelers[i].y] = 0;
                        travelers[i].x = travelers[i].x+1;
                        travelers[i].y = travelers[i].y;
                    }
                case 3:
                    if travelers[i].y<len(grid)-1 && grid[travelers[i].x][travelers[i].y+1] == 0{
                        grid[travelers[i].x][travelers[i].y+1] = grid[travelers[i].x][travelers[i].y];
                        grid[travelers[i].x][travelers[i].y] = 0;
                        travelers[i].x = travelers[i].x;
                        travelers[i].y = travelers[i].y+1;
                    }
                }
                //fmt.Println("Ściagnalem ruch", travelers[i].id);
            }
        }
    }
}

func camera(grid [][]int){
    for{
    for i:=0; i<len(grid); i++{
        for j:=0; j<len(grid); j++{
            if grid[i][j]!= 0{
                fmt.Printf("%02d ", grid[i][j]);
            } else{
                fmt.Printf("-- ");
            }
        }
        fmt.Println();
    }
    fmt.Println();
    time.Sleep(time.Second * 3);
}
}
Copy after login

I'm a little overwhelmed by all the ideas - wgs, mutexes, atoms, etc.

Solution

  • If you want to process work simultaneously (for example, taking a camera snapshot and moving a traveler can happen at the same time), goroutines are lightweight threads.
  • Channels are used to transfer data between Go routines.
  • Mutexes are used to allow goroutines to add locks on shared data for exclusive data access to avoid race conditions.

That being said:

  • Running the camera snapshot in one goroutine while having the traveler move around in another goroutine looks good. Spawning the Goroutine is not necessary, you only need to do it once, so you can execute it in the main Goroutine.
  • In your case, the channel brings no benefit. You have a Goroutine that generates a message and sends it over a channel to another Goroutine that will do the movement. You can do all of this sequentially in a single goroutine and avoid unnecessary complexity. Channels are useful for different use cases, but here it is redundant.
  • Since you have two goroutines accessing shared memory (the grid), you need a mutex to avoid race conditions. Whenever one of these runs, it must "lock", do its work, and then "unlock". Another goroutine will block at the lock step until the first goroutine that acquired the lock unlocks. You can further optimize it using read/write locks (only the camera needs a read lock, while the mobile coroutine needs a read/write lock)
  • If you want more randomness, you can create a goroutine for each traveler.

The above is the detailed content of A simple Go simulation - concurrency issues. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template