-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(coroutine): Added `Concurrent` and `Parallel` Signed-off-by: Flc゛ <[email protected]> * feat(coroutine): Added `Concurrent` and `Parallel` Signed-off-by: Flc゛ <[email protected]> * feat(coroutine): Added `Concurrent` and `Parallel` Signed-off-by: Flc゛ <[email protected]> --------- Signed-off-by: Flc゛ <[email protected]>
- Loading branch information
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package coroutine | ||
|
||
import "sync" | ||
|
||
func Concurrent(limit int, tasks ...func()) { | ||
var ( | ||
wg sync.WaitGroup | ||
ch = make(chan struct{}, limit) | ||
) | ||
defer close(ch) | ||
wg.Add(len(tasks)) | ||
|
||
for _, task := range tasks { | ||
ch <- struct{}{} | ||
go func(task func()) { | ||
defer func() { | ||
<-ch | ||
wg.Done() | ||
}() | ||
task() | ||
}(task) | ||
} | ||
|
||
wg.Wait() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package coroutine | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConcurrent(t *testing.T) { | ||
var ( | ||
start = time.Now() | ||
buffer bytes.Buffer | ||
mu sync.Mutex | ||
) | ||
|
||
Concurrent(2, func() { | ||
time.Sleep(1 * time.Second) | ||
mu.Lock() | ||
defer mu.Unlock() | ||
buffer.WriteString("1") | ||
}, func() { | ||
time.Sleep(2 * time.Second) | ||
mu.Lock() | ||
defer mu.Unlock() | ||
buffer.WriteString("2") | ||
}, func() { | ||
time.Sleep(3 * time.Second) | ||
mu.Lock() | ||
defer mu.Unlock() | ||
buffer.WriteString("3") | ||
}) | ||
|
||
assert.Equal(t, "123", buffer.String()) | ||
assert.True(t, time.Since(start) > 4*time.Second) | ||
assert.True(t, time.Since(start) < 6*time.Second) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package coroutine | ||
|
||
import "sync" | ||
|
||
func Parallel(tasks ...func()) { | ||
var wg sync.WaitGroup | ||
wg.Add(len(tasks)) | ||
|
||
for _, task := range tasks { | ||
go func(task func()) { | ||
defer wg.Done() | ||
task() | ||
}(task) | ||
} | ||
|
||
wg.Wait() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package coroutine | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParallel(t *testing.T) { | ||
var ( | ||
start = time.Now() | ||
buffer bytes.Buffer | ||
mu sync.Mutex | ||
) | ||
|
||
Parallel(func() { | ||
time.Sleep(1 * time.Second) | ||
mu.Lock() | ||
defer mu.Unlock() | ||
buffer.WriteString("1") | ||
}, func() { | ||
time.Sleep(2 * time.Second) | ||
mu.Lock() | ||
defer mu.Unlock() | ||
buffer.WriteString("2") | ||
}, func() { | ||
time.Sleep(3 * time.Second) | ||
mu.Lock() | ||
defer mu.Unlock() | ||
buffer.WriteString("3") | ||
}) | ||
|
||
assert.Equal(t, "123", buffer.String()) | ||
assert.True(t, time.Since(start) < 4*time.Second) | ||
} |
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
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