Tcl Source Code

Artifact [cd653ea4f0]
Login

Artifact cd653ea4f0ae03d30d1f727d526b9d3965f3671a:

Attachment "test1.c" to ticket [406058ffff] added by nobody 2001-05-11 05:18:42.
/*
Under deeper inspection of your code, I'd like to edit it for better
cross-platform support of the threading macros in tcl.h.  Honestly,
casting of function types is evil.  Especially the calling convention
part.  I don't think I'm being nit-picky because the compiler will emit
different different machine code for __cdecl and __stdcall.  Try it and
look at the difference of the stack handling of the listing .asm file.
*/

#define TCL_THREADS 1
#include <tcl.h>

#ifdef _MSC_VER
#   ifdef _DEBUG
#	pragma comment (lib, "tcl" STRINGIFY(JOIN(TCL_MAJOR_VERSION,TCL_MINOR_VERSION)) "dt.lib")
#   else
#	pragma comment (lib, "tcl" STRINGIFY(JOIN(TCL_MAJOR_VERSION,TCL_MINOR_VERSION)) "t.lib")
#   endif
#endif

Tcl_ThreadCreateType
ThreadMainProc(ClientData clientData)
{
	int *I = (int *) clientData;
	*I = 0;

	// if Tcl_ExitThread() is not called, Tcl_FinalizeThread() isn't
	// called either.  That can lead to a large resource leak depending
	// on how much of the Tcl API is initialized.
	Tcl_ExitThread(0);

	// This just makes the compiler happy.
	TCL_THREAD_CREATE_RETURN;
}

int main(int argc, char *argv[])
{
	int I = 1;
	Tcl_ThreadId* ThreadId = NULL;

	Tcl_FindExecutable(argv[0]);

	// At this point the number of handles is 14 and of threads 1.

	if( Tcl_CreateThread(
			ThreadId, 
			ThreadMainProc,
			&I,
			TCL_THREAD_STACK_DEFAULT,
			TCL_THREAD_NOFLAGS ) != TCL_OK)
	{
		return 1;	// Error handle
	}

	// This loop is finished by the thread.
	// At this point the number of handles is 15 and of threads 2.
	while( I )
		printf( "%d", I );

	// At this point the number of handles is 15 and of threads 1, i.e. the thread is dead.

	return 0;
}