Created
July 24, 2015 14:28
-
-
Save kingluo/eade913c2c9dd8812912 to your computer and use it in GitHub Desktop.
schedule performance between goroutine and pthread
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
king@debian:~$ time ./a.out | |
real 0m10.261s | |
user 0m3.040s | |
sys 0m17.312s | |
king@debian:~$ time go run pthread.go | |
real 0m4.017s | |
user 0m5.444s | |
sys 0m1.052s |
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
#include <pthread.h> | |
#include <stdio.h> | |
void* thread_func(void* arg) | |
{ | |
int i; | |
for (i = 0; i < 20000; i++) { | |
pthread_yield(); | |
} | |
return NULL; | |
} | |
int main() | |
{ | |
const int max_threads = 1024; | |
pthread_t pth[max_threads]; | |
int i; | |
for (i=0;i<max_threads;i++) { | |
pthread_create(&pth[i], NULL, thread_func, NULL); | |
} | |
for (i=0;i<max_threads;i++) { | |
pthread_join(pth[i], NULL); | |
} | |
} |
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 "runtime" | |
func thread_func(done chan struct{}) { | |
for i := 0; i < 20000; i++ { | |
runtime.Gosched() | |
} | |
done <- struct{}{} | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
max_threads := 1024 | |
done := make(chan struct{}) | |
for i := 0; i < max_threads; i++ { | |
go thread_func(done) | |
} | |
for i := 0; i < max_threads; i++ { | |
<-done | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment