Re: Own implementation of multithread



lkfstephen@xxxxxxxxx wrote:
Dear all,

I am working on a multithread application.
I have already built the TCL using --enable-threads.

In c++ code, I am trying to create many threads, each thread will
create its own interpreter.
Each interpreter will need to eval a script.

For example,

void * func( void * null ){
Tcl_Interp* tcl_interp = Tcl_CreateInterp();
Tcl_Init(tcl_interp);
char * script = "set a 1\n"
"set b 2\n"
"puts $a\n"
"puts $b\n";
Tcl_EvalObjEx(tcl_interp, script , TCL_EVAL_GLOBAL);
delete tcl_interp;
}

"delete tcl_interp" is almost certainly wrong. The correct way is:
Tcl_DeleteInterp(interp);

I also suggest not using a tcl prefix for most things. While it's
unlikely that tcl would ever define something with the name tcl_interp,
it's possible.


int main (){
...
for (int i = 0; i < 10000; ++i)
pthread_create(&tid,NULL,func,NULL);
...
}

I would like to ask is this approach is wrong?

Stephen Lai

You're creating 10000 threads. That's a lot of threads. Most systems
have a limit much less than that. You'll most likely run out RAM, due
to the stack space for each thread, and other data structures. You're
also neglecting to check for errors. I have a feeling your
pthread_create is returning an error before 10000.

There is a PTHREAD_THREADS_MAX that you might want to look into:
http://www.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html

There is a problem though with that approach though, and that problem is
that GNU/Linux doesn't define PTHREAD_THREADS_MAX, because it's not
fully POSIX compliant/compatible.


George
.



Relevant Pages

  • Own implementation of multithread
    ... I have already built the TCL using --enable-threads. ... Each interpreter will need to eval a script. ... void * func{ ...
    (comp.lang.tcl)
  • Tcl thread local storage memory problem
    ... I am working on an application that originally uses one thread, TCL ... and the C-library to initialize an interpreter and executing standard ... InitializeThreadLocalStorage(); /*If no threads, initialize ... able to get Tcl thread local storage ...
    (comp.lang.tcl)
  • Re: File data format
    ... This is how I always look at data processing with Tcl: ... then let the very effecient tcl interpreter do the parsing. ... Create procs that match the data keys. ... date, commit, and compile, that matched the same signature, then your ...
    (comp.lang.tcl)
  • Re: No argv, argc for worker thread
    ... I expected that a Tcl interpreter started in any manner would accept options ... This is how the interpreter works with command line options ... a "main" script processes command line ...
    (comp.lang.tcl)
  • Re: Tcl/TK is indeed dying
    ... A fat Tcl interpreter has impact to all wanting to see Tcl linked to ... other software and embedded in hardware, because fatness make it less ...
    (comp.lang.tcl)