Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 3bb7c61f rédigé par E214194U's avatar E214194U
Parcourir les fichiers

f

parent 39dc252d
Branches
Étiquettes
Aucune requête de fusion associée trouvée
Affichage de
avec 349 ajouts et 0 suppression
Fichier déplacé
Fichier déplacé
Fichier déplacé
Fichier déplacé
Fichier déplacé
package main
import "fmt"
func main() {
var c chan bool = make(chan bool, 3)
c <- true
c <- false
fmt.Println(<-c)
fmt.Println(<-c)
}
package main
import "fmt"
func iter(ID int) {
for i := 0; i < 100; i++ {
fmt.Println("Goroutine", ID, "itération", i)
}
}
func main() {
for i:=1; i<=100; i++ {
go iter(i)
}
iter(100)
}
package main
import "fmt"
var turn1 bool
func routine1(c1, c2 chan bool) {
for {
<-c1
fmt.Println("routine1")
c2 <- true
}
}
func routine2(c1, c2 chan bool) {
for {
<-c2
fmt.Println("routine2")
c1 <- true
}
}
func main() {
var turn1 chan bool = make(chan bool, 0)
var turn2 chan bool = make(chan bool, 0)
go routine1(turn1, turn2)
turn1 <- true
routine2(turn1, turn2)
}
package main
import (
"fmt"
"math/rand"
"time"
)
func routine(c chan int) {
wait := rand.Intn(1000)
time.Sleep(time.Duration(wait) * time.Millisecond)
c <- wait
}
func main() {
rand.Seed(int64(time.Now().Nanosecond()))
c1 := make(chan int)
c2 := make(chan int)
go routine(c1)
go routine(c2)
cTimeOut := time.After(500 * time.Millisecond)
// le premier cannal qui va être asigné va désigner le cas du select qui va être exécuter
select {
case w := <-c1:
fmt.Println("La première goroutine a été la plus rapide.")
fmt.Println("Elle a mis", w, "millisecondes.")
case w := <-c2:
fmt.Println("La deuxième goroutine a été la plus rapide.")
fmt.Println("Elle a mis", w, "millisecondes.")
case <-cTimeOut:
fmt.Println("Délai expiré")
}
}
package main
import (
"fmt"
"time"
)
func writer(c chan bool) {
for {
time.Sleep(500 * time.Millisecond)
c <- true
fmt.Println("Écriture")
}
}
func reader(c chan bool) {
for {
time.Sleep(time.Second)
<-c
fmt.Println("Lecture")
}
}
func main() {
var c chan bool = make(chan bool)
go writer(c)
reader(c)
}
package main
import (
"fmt"
"sync"
)
var x, y int
var w sync.WaitGroup
var mutex sync.Mutex
func switchxy() {
mutex.Lock()
for i := 0; i < 1000; i++ {
x, y = y, x
}
w.Done()
mutex.Unlock()
}
func main() {
x = 5
y = 7
for i := 0; i < 1000; i++ {
w.Add(1)
go switchxy()
}
w.Wait()
fmt.Println("x vaut", x, "et y vaut", y)
}
package main
import "fmt"
// calcule n puissance m
func puissance(n, m int) (res int) {
res = 1
for i := 0; i < m; i++ {
res *= n
}
return
}
func main() {
for i := 0; i < 10; i++ {
for j := 0; j < 3; j++ {
fmt.Println(i, "puissance", j, "vaut", puissance(i, j))
}
}
}
package main
import (
"fmt"
"sync"
)
var w sync.WaitGroup
// calcule n puissance m
func puissance(n, m int) {
var res int = 1
for i := 0; i < m; i++ {
res = n
}
fmt.Println(n, "puissance", m, "vaut", res)
w.Done()
return
}
func main() {
for i := 0; i < 10; i++ {
for j := 0; j < 3; j++ {
w.Add(1)
go puissance(i, j)
}
}
w.Wait()
}
package main
import "fmt"
var res int
// calcule n puissance m
func puissance(n, m int) {
res = 1
for i := 0; i < m; i++ {
res *= n
}
fmt.Println(n, "puissance", m, "vaut", res)
return
}
func main() {
for i := 0; i < 10; i++ {
for j := 0; j < 3; j++ {
puissance(i, j)
}
}
}
package main
import (
"fmt"
"sync"
)
var res int
var w sync.WaitGroup
var mutex sync.Mutex
// calcule n puissance m
func puissance(n, m int) {
mutex.Lock()
res = 1
for i := 0; i < m; i++ {
res *= n
}
fmt.Println(n, "puissance", m, "vaut", res)
w.Done()
mutex.Unlock()
}
func main() {
for i := 0; i < 10; i++ {
for j := 0; j < 3; j++ {
w.Add(1)
go puissance(i, j)
}
}
w.Wait()
}
package main
import (
"fmt"
"sync"
)
var w sync.WaitGroup
var mutex sync.Mutex
// calcule n puissance m
func puissance(n, m int, res *int) {
mutex.Lock()
*res = 1
for i := 0; i < m; i++ {
*res *= n
}
fmt.Println(n, "puissance", m, "vaut", *res)
w.Done()
mutex.Unlock()
}
func main() {
var res int
for i := 0; i < 10; i++ {
for j := 0; j < 3; j++ {
w.Add(1)
go puissance(i, j, &res)
}
}
w.Wait()
}
package main
import (
"log"
"sync"
)
var com1, com2 []int
var w sync.WaitGroup
func prod(n int) {
defer w.Done()
for i := 0; i < n; i++ {
com1 = append(com1, i)
}
}
func trans() {
defer w.Done()
for {
if len(com1) > 0 {
com2 = append(com2, com1[0])
com1 = com1[1:]
}
}
}
func cons() {
defer w.Done()
for {
if len(com2) > 0 {
log.Print(com2[0])
com2 = com2[1:]
}
}
}
func main() {
w.Add(3)
go prod(1000)
go trans()
go cons()
//w.Wait()
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter