OriginalBugID: 4193 Bug
Version: 8.3
SubmitDate: '2000-02-16'
LastModified: '2000-02-22'
Severity: SER
Status: Closed
Submitter: techsupp
ChangedBy: hobbs
RelatedBugIDs: 4199
OS: Windows NT
Machine: Win NT SP 5, 32 Mg and AIX 4.3
FixedDate: '2000-10-25'
ClosedDate: '2000-02-22'
Name:
jean ANDRE
Comments:
Sample code to have a beter understanding of my problem.
---------------------------Main program--------------------------
#include "base_interp.h"
#include "main_interp.h"
#include "slave_interp.h"
static int c_main_interp( int islave, char* scriptfile );
int main( int argc, char *argv[] ) {
register i=0;
printf("\nBienvenue dans le programme de test du TCL\n" );
c_main_interp( 1, "C:\\toto.tcl" );
return 0;
}
static int c_main_interp( int islave, char* scriptfile ) {
CMainInterpreter MyInterp( islave, scriptfile );
MyInterp.CreateSlavesInterp();
Sleep(5000);
MyInterp.DeleteSlavesInterp();
return 0;
}
/* c_main_interp */
-------------------------------------------------------------------------
---
-------------------------------Master intepreter
manager-------------------
----------------------------------Sample demo
code--------------------------
-------------------This code show the creation of X slave
interprer-------
-------------------------------------------------------------------------
--
LONG CMainInterpreter::CreateSlavesInterp( VOID ) {
register i;
CSlaveInterpreter * pSlaveInterp = NULL;
/* reset error msg */
SetErrorMsg();
/* check if slave are already created */
if ( m_paSlaveInterp != NULL )
return SetErrorMsg( TCL_ERROR, "Slaves interpreters already created!"
);
if ( m_sNbSlave <= 0 )
return SetErrorMsg( TCL_ERROR, "Bad interpreter number, must be > 0!"
);
/* alloc enough space to store object slave with sentinelle */
/* this is a heterogenous container low level implemetation */
if ( (m_paSlaveInterp = (CBaseInterpreter **)calloc( m_sNbSlave+1,
sizeof( CBaseInterpreter *) )) == NULL )
return SetErrorMsg( TCL_ERROR, "Not enough memory to create slave
interpreters!" );
/* create all slaves required */
for ( i=0; i<m_sNbSlave; i++ ) {
if ( (pSlaveInterp = new CSlaveInterpreter( m_szInitScriptFile )) ==
NULL )
return SetErrorMsg( TCL_ERROR, "Cannot create slave interpreter
object!" );
m_paSlaveInterp[i] = pSlaveInterp;
pSlaveInterp->CreateThreadedInterp();
}
return m_lRc;
}
/* eof CreateSlavesInterp */
-------------------------------------------------------------------------
---
------------------Slave interpreter implementation and definition
--------
----------------------------------Sample demo
code--------------------------
-------------------This code show the thread creation -------
-------------------------------------------------------------------------
--
/*
*---------------------------------------------------------------------
*S L A V E I N T E R P R E T E RC L A S S
*---------------------------------------------------------------------
*/
class DLL_EXPORT CSlaveInterpreter : public CBaseInterpreter {
private:
Tcl_ThreadIdm_threadId;
Tcl_Conditionm_condWait;
Tcl_Mutexm_mutexLock;
HANDLEm_hThread;
public:
CSlaveInterpreter( VOID );
CSlaveInterpreter( PSZ pszInitScriptFile );
~CSlaveInterpreter( VOID );
/* get methods */
/* set methods */
/* slaves interpreter methods */
LONG CreateThreadedInterp( VOID );
VOID TclMutexLock( VOID );
VOID TclMutexUnlock( VOID );
VOID TclConditionWait( VOID );
VOID TclConditionNotify( VOID );
protected:
VOID Initialize( VOID );
};
#endif /* __cplusplus */
#endif
/* eof */
--------------------------------------------------------------------
---------------------------Implementation---------------------------
--------------------------------------------------------------------
#include "slave_interp.h"
/* with such global variable, TCL finalize is ok ! */
TCL_DECLARE_MUTEX(mutexLock);
static Tcl_Condition condWait;
/* but when this variables are in object is not */
/* this the main of the thread */
/* clientdata is the pointer of the object who create thread wich
contains */
/* mutex and cond. So we should not need to use "specific thread data"
*/
/* cause all is done by the object ? */
static void fnInterpreter( LPVOID clientdata );
/*
*---------------------------------------------------------------------
*S L A V EI N T E R P R E T E R C L A S S
*---------------------------------------------------------------------
*/
/* explicit constructor */
CSlaveInterpreter::CSlaveInterpreter( VOID ) : CBaseInterpreter() {
Initialize();
}
/* CSlaveInterpreter */
CSlaveInterpreter::CSlaveInterpreter( PSZ pszInitScriptFile ) : CBaseInterpreter( pszInitScriptFile ) {
Initialize();
}
/* eof CSlaveInterpreter */
CSlaveInterpreter::~CSlaveInterpreter( VOID ) {
/* this function doesn't exist ? */
//TclFinalizeCondition(&(m_condWait));
//TclFinalizeMutex(&(m_mutexLock));
/* free handle, etc....*/
}
/* ~CSlaveInterpreter */
inline VOID CSlaveInterpreter::Initialize( VOID ) {
m_hThread = NULL;
m_threadId = NULL;
m_condWait = NULL;
m_mutexLock = NULL;
/* etc....*/
}
/* CSlaveInterpreter */
LONG CSlaveInterpreter::CreateThreadedInterp( VOID ) {
TclMutexLock();
m_hThread = CreateThread( NULL/* security attributes*/
, 0/* stack size*/
, (LPTHREAD_START_ROUTINE)fnInterpreter /* main proc*/
, (LPVOID) this/* clientdata */
, 0 /* creation flags*/
, (DWORD*)m_threadId );/* address of return id */
if ( m_hThread == NULL ) {
SetErrorMsg( TCL_ERROR, "Cannot create thread" );
}
else {
/* return until the thread is completly started */
m_condWait = NULL;
TclConditionWait();
SetErrorMsg();
}
TclMutexUnlock();
return m_lRc;
}
/* CreateThreadedInterp */
inline VOID CSlaveInterpreter::TclMutexLock( VOID ) {
Tcl_MutexLock( &(m_mutexLock) );
}
/* TclMutexLock */
inline VOID CSlaveInterpreter::TclMutexUnlock( VOID ) {
Tcl_MutexUnlock( &(m_mutexLock) );
}
/* TclMutexUnLock */
inline VOID CSlaveInterpreter::TclConditionWait( VOID ) {
Tcl_ConditionWait( &(m_condWait), &(m_mutexLock), NULL );
}
/* TclConditionWait */
inline VOID CSlaveInterpreter::TclConditionNotify( VOID ) {
Tcl_ConditionNotify( &(m_condWait) );
}
/* TclConditionNotify */
static void fnInterpreter( LPVOID clientdata ) {
int i=0;
CSlaveInterpreter * pSlaveInterpreter;
pSlaveInterpreter = (CSlaveInterpreter*) clientdata;
pSlaveInterpreter->TclMutexLock();
pSlaveInterpreter->CreateInterpreter();
pSlaveInterpreter->TclConditionNotify();
pSlaveInterpreter->TclMutexUnlock();
pSlaveInterpreter->DeleteInterpreter();
Tcl_ExitThread( 0 );
}
/* CreateThreadedInterp */
/* eof */
ReproducibleScript:
Hello scriptics team,
I'm not really sure if it's a bug or if I don't use Tcl correctly.
I'm going to explain you my problem, but my english as you will see is
not my native language... So don't hesitate to email me if you don't
understand me.
I've download the source code tcl830 and compile it with VC++ 6.0 to allow multi-threading. I'm program in object oriented and I try to allow an
application to run several threads, each threads run only one interpreter.
I've created a class manager that create "slave classes" wich create a thread and an intepreter. All these object are in DLL.
I made a main program that accept as parameter a number a slave to create
and an init TCL script file to execute.
I synchronized slave object with is not available until the thread and its interpreter are ready.
The problem is at the end of main (after tue exit of the main) program, TCL make's an acces violation. The problem doesn't appear when I declare condition variable and mutex at the globale scope of the object. My point of view is TCL core try to deallocate condition variable and mutex while the object was that maintain condition variable and mutex adress are not available because object was already destroyed automatically by the destructor when the program terminate.
If I look the sample code from Tcl811 (tclThreadTest.c), you use TclFinalizeCondition (should have also TclFinalizeMutex) to free the
ressource manually (in my case in the destructor of the object). This
function in not available in export !
How to deallocate manually Mutex and Condition ? They cannot be private/ local to the object ?
In fact there another work around wich is to call directly Win API or PThread API !
Thank you very much.
Jean.
|