github.com/bir3/gocompiler@v0.9.2202/src/cmd/cgo/internal/test/cthread_windows.c (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  #define WIN32_LEAN_AND_MEAN
     6  #include <windows.h>
     7  #include <process.h>
     8  #include "_cgo_export.h"
     9  
    10  __stdcall
    11  static unsigned int
    12  addThread(void *p)
    13  {
    14  	int i, max;
    15  	
    16  	max = *(int*)p;
    17  	for(i=0; i<max; i++)
    18  		Add(i);
    19  	return 0;
    20  }
    21  
    22  void
    23  doAdd(int max, int nthread)
    24  {
    25  	enum { MaxThread = 20 };
    26  	int i;
    27  	uintptr_t thread_id[MaxThread];
    28  	
    29  	if(nthread > MaxThread)
    30  		nthread = MaxThread;
    31  	for(i=0; i<nthread; i++)
    32  		thread_id[i] = _beginthreadex(0, 0, addThread, &max, 0, 0);
    33  	for(i=0; i<nthread; i++) {
    34  		WaitForSingleObject((HANDLE)thread_id[i], INFINITE);
    35  		CloseHandle((HANDLE)thread_id[i]);
    36  	}
    37  }
    38  
    39  __stdcall
    40  static unsigned int
    41  goDummyCallbackThread(void* p)
    42  {
    43  	int i, max;
    44  
    45  	max = *(int*)p;
    46  	for(i=0; i<max; i++)
    47  		goDummy();
    48  	return 0;
    49  }
    50  
    51  int
    52  callGoInCThread(int max)
    53  {
    54  	uintptr_t thread_id;
    55  	thread_id = _beginthreadex(0, 0, goDummyCallbackThread, &max, 0, 0);
    56  	WaitForSingleObject((HANDLE)thread_id, INFINITE);
    57  	CloseHandle((HANDLE)thread_id);
    58  	return max;
    59  }