Skip to content

Commit

Permalink
Disallow setting more threads that number of CPU cores available [wip]
Browse files Browse the repository at this point in the history
Signed-off-by: Lukasz Gryglicki <[email protected]>
  • Loading branch information
lukaszgryglicki committed Nov 6, 2019
1 parent 27058a1 commit 3cb7534
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
func GetThreadsNum(ctx *Ctx) int {
// Use environment variable to have singlethreaded version
if ctx.NCPUs > 0 {
n := runtime.NumCPU()
if ctx.NCPUs > n {
ctx.NCPUs = n
}
runtime.GOMAXPROCS(ctx.NCPUs)
return ctx.NCPUs
}
Expand Down
1 change: 1 addition & 0 deletions threads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestGetThreadsNum(t *testing.T) {
{ST: true, NCPUs: 1, expected: 1},
{ST: true, NCPUs: -1, expected: 1},
{ST: true, NCPUs: 2, expected: 2},
{ST: true, NCPUs: nThreads + 1, expected: nThreads},
}
// Execute test cases
for index, test := range testCases {
Expand Down

0 comments on commit 3cb7534

Please sign in to comment.