Skip to content

Commit af77e05

Browse files
committed
chapter4 review
1 parent 969bf6f commit af77e05

File tree

6 files changed

+85
-34
lines changed

6 files changed

+85
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a.out
2+

chapter4/channel.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package main
2+
23
import "fmt"
4+
35
func Count(ch chan int) {
4-
ch <- 1
5-
fmt.Println("Counting")
6+
fmt.Println("Counting")
7+
ch <- 1
68
}
9+
710
func main() {
8-
chs := make([]chan int, 10)
9-
for i := 0; i < 10; i++ {
10-
chs[i] = make(chan int)
11-
go Count(chs[i])
12-
}
13-
for _, ch := range(chs) {
14-
<- ch
15-
}
16-
}
11+
chs := make([]chan int, 10)
12+
for i := 0; i < 10; i++ {
13+
chs[i] = make(chan int)
14+
go Count(chs[i])
15+
}
16+
for _, ch := range chs {
17+
<-ch
18+
}
19+
}

chapter4/goroutine.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package main
2+
23
import "fmt"
4+
35
func Add(x, y int) {
46
z := x + y
5-
fmt.Println(z)
7+
fmt.Println(z)
68
}
9+
710
func main() {
8-
for i := 0; i < 10; i++ {
9-
go Add(i, i)
11+
for i := 0; i < 10; i++ {
12+
go Add(i, i)
1013
}
11-
}
14+
}

chapter4/goroutine2.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
package main
2+
23
import "fmt"
34
import "sync"
45
import "runtime"
6+
57
var counter int = 0
8+
69
func Count(lock *sync.Mutex) {
7-
lock.Lock()
8-
counter++
9-
fmt.Println(counter) //fmt.Println(z)
10-
lock.Unlock()
10+
lock.Lock()
11+
counter++
12+
fmt.Println(counter) //fmt.Println(z)
13+
lock.Unlock()
1114
}
15+
1216
func main() {
13-
lock := &sync.Mutex{}
14-
for i := 0; i < 10; i++ {
15-
go Count(lock)
17+
lock := &sync.Mutex{}
18+
for i := 0; i < 10; i++ {
19+
go Count(lock)
1620
}
17-
for {
18-
lock.Lock()
19-
c := counter
20-
lock.Unlock()
21-
runtime.Gosched()
22-
if c >= 10 {
23-
break
21+
for {
22+
lock.Lock()
23+
c := counter
24+
lock.Unlock()
25+
runtime.Gosched()
26+
if c >= 10 {
27+
break
2428
}
2529
}
26-
}
30+
}

chapter4/thread.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <pthread.h>
4+
5+
void *count();
6+
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
7+
int counter = 0;
8+
9+
main()
10+
{
11+
int rc1, rc2;
12+
pthread_t thread1, thread2;
13+
14+
/* 创建线程,每个线程独立执行函数functionC */
15+
16+
if((rc1 = pthread_create(&thread1, NULL, &count, NULL)))
17+
{
18+
printf("Thread creation failed: %d\n", rc1);
19+
}
20+
21+
if((rc2 = pthread_create(&thread2, NULL, &count, NULL)))
22+
{
23+
printf("Thread creation failed: %d\n", rc2);
24+
}
25+
26+
/* 等待所有线程执行完毕 */
27+
pthread_join( thread1, NULL);
28+
pthread_join( thread2, NULL);
29+
30+
exit(0);
31+
}
32+
33+
void *count()
34+
{
35+
pthread_mutex_lock( &mutex1 );
36+
counter++;
37+
printf("Counter value: %d\n",counter);
38+
pthread_mutex_unlock( &mutex1 );
39+
}
40+

env.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export GOPATH=$GOPATH:$QINIUROOT/gobook/chapter1/calcproj
2-
export GOPATH=$GOPATH:$QINIUROOT/gobook/chapter2/sorter
3-
export PATH=$PATH:$QINIUROOT/gobook/chapter1/calcproj/bin
4-
export PATH=$PATH:$QINIUROOT/gobook/chapter2/sorter/bin
1+
export GOPATH=$GOPATH:$GOBOOK/chapter1/calcproj:$GOBOOK/chapter2/sorter
2+
export PATH=$PATH:$GOBOOK/chapter1/calcproj/bin:$GOBOOK/chapter2/sorter/bin
3+

0 commit comments

Comments
 (0)