Created
January 18, 2020 08:38
-
-
Save fujim2/dc3625b8f20fc5eb40c3fb91e3d15356 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "time" | |
var Thread0inside int = 0; | |
var Thread1inside int = 0; | |
func ThreadZero() { | |
for i:=0; i<10; i++{ | |
for Thread1inside == 1 {} | |
//time.Sleep(time.Nanosecond) | |
Thread0inside = 1; | |
CriticalSectionZero() | |
Thread0inside = 0; | |
OtherStuffZero(); | |
} | |
} | |
func ThreadOne() { | |
for i:=0; i<10; i++{ | |
for Thread0inside == 1 {} | |
//time.Sleep(time.Nanosecond) | |
Thread1inside = 1; | |
CriticalSectionOne() | |
Thread1inside = 0; | |
OtherStuffOne(); | |
} | |
} | |
func CriticalSectionZero() { | |
for r:=0; r<5;r++ { | |
fmt.Printf("CriticalSectionZero(%d)\n", r) | |
time.Sleep(100*time.Millisecond) | |
} | |
} | |
func CriticalSectionOne() { | |
for r:=0; r<5;r++ { | |
fmt.Printf(" CriticalSectionOne(%d)\n", r) | |
time.Sleep(100*time.Millisecond) | |
} | |
} | |
func OtherStuffZero() { | |
fmt.Printf("OtherStuffZero\n") | |
} | |
func OtherStuffOne() { | |
fmt.Printf(" OtherStuffOne\n") | |
} | |
func main() { | |
go ThreadOne() | |
go ThreadZero() | |
time.Sleep(10000*time.Millisecond) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment