Skip to content

Instantly share code, notes, and snippets.

@kingluo
Created July 24, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingluo/eade913c2c9dd8812912 to your computer and use it in GitHub Desktop.
Save kingluo/eade913c2c9dd8812912 to your computer and use it in GitHub Desktop.
schedule performance between goroutine and pthread
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
#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);
}
}
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