1

I have a really minor doubt Suppose there are three func A,B,C C is being called from both A and B I am running A and B on different threads , will it result in conflict any time in feature when it calls C within it

for reference i am adding this code

package main

import (
"fmt"
)

func xyz() {
     for true {
         fmt.Println("Inside xyz")
         call("xyz")
     }
}

func abc() {
    for true {
         fmt.Println("Inside abc")
         call("abc")
    }
}

func call(s string) {
    fmt.Println("call from " + s)
}

func main() {
    go xyz()
    go abc()
    var input string
    fmt.Scanln(&input)
}

Here A = xyz(), B = abc(), C = call()

will there be any conflict or any runtime error in future while running these two go routines

Ezio
  • 723
  • 6
  • 14

1 Answers1

1

Whether multiple goroutines are safe to run concurrently or not comes down to whether they share data without synchronization. In this example, both abc and xyz print to stdout using fmt.Println, and call the same routine, call, which prints to stdout using fmt.Println. Since fmt.Println doesn't use synchronization when printing to stdout, the answer is no, this program is not safe.

Community
  • 1
  • 1
janos
  • 120,954
  • 29
  • 226
  • 236
  • what if i am not printing anything to stdout and instead doing something like downloading a file ? – Ezio Oct 29 '16 at 09:58
  • As long as the two methods don't access shared data, they can be safe – janos Oct 29 '16 at 10:08
  • My function is sharing a database variable which helps to connect to different tables in the database , but a call from any function access only a particular table in the database other that what other functions are accessing – Ezio Oct 29 '16 at 10:45
  • This is starting to look like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)... Methods accessing shared data can be safe if they use synchronization. Database that allow concurrent access use synchronization, so yes, you can probably use a database safely by concurrent goroutines. – janos Oct 29 '16 at 11:05