github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/misc/cgo/test/cthread.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cgotest 6 7 // extern void doAdd(int, int); 8 import "C" 9 10 import ( 11 "sync" 12 "testing" 13 ) 14 15 var sum struct { 16 sync.Mutex 17 i int 18 } 19 20 //export Add 21 func Add(x int) { 22 defer func() { 23 recover() 24 }() 25 sum.Lock() 26 sum.i += x 27 sum.Unlock() 28 var p *int 29 *p = 2 30 } 31 32 func testCthread(t *testing.T) { 33 sum.i = 0 34 C.doAdd(10, 6) 35 36 want := 10 * (10 - 1) / 2 * 6 37 if sum.i != want { 38 t.Fatalf("sum=%d, want %d", sum.i, want) 39 } 40 }