[Mono-devel-list] Multiple application domains

Casper Hornstrup chorns at users.sourceforge.net
Tue Apr 22 16:28:55 EDT 2003

This test is performed using Mono compiled from CVS HEAD as of today.
However, I also have Mono installed from latest rpm release.

I'm trying to run a Mono executable in a separate application domain
created using mono_domain_create().

If I run the application below, then it will work as expected. It will
"Test..." on the console. However, if I change the second #if
from #if 1 to #if 0, then the application hangs before displaing
Changing the first #if conditional has no effect.

Is multiple application domains not supported or am I doing this
incorrectly ?

Also, is mono_jit_cleanup() supposed to be called for each value
by mono_domain_create() ?

export PATH=/home/cvs/mono/install/bin:$PATH
gcc -v -L/home/cvs/mono/install/lib -I/home/cvs/mono/install/include \
  -g -o teste teste.c `pkg-config --cflags --libs mono` -lm
mcs test.cs
./teste test.exe

Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --host=i386-redhat-linux --with-system-zlib
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/cc1 -lang-c -v
-I/home/cvs/mono/install/include -I/usr/include/glib-2.0
-I/usr/lib/glib-2.0/include -D__GNUC__=3 -D__GNUC_MINOR__=2
-D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__
-D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1
-Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__
-DMONO_USE_EXC_TABLES -D_REENTRANT teste.c -quiet -dumpbase teste.c -g
-version -fexceptions -o /tmp/ccFbBEpg.s
GNU CPP version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (cpplib) (i386
GNU C version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (i386-redhat-linux)
        compiled by GNU C version 3.2 20020903 (Red Hat Linux 8.0
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
End of search list.
 as -V -Qy -o /tmp/ccpTBYwF.o /tmp/ccFbBEpg.s
GNU assembler version (i386-redhat-linux) using BFD version 20020802
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/collect2 --eh-frame-hdr -m
elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o teste
-L/home/cvs/mono/install/lib -L/usr/lib/gcc-lib/i386-redhat-linux/3.2
-L/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../.. /tmp/ccpTBYwF.o
--export-dynamic -lmono -lpthread -lm -lgmodule-2.0 -ldl -lglib-2.0 -lm
-lgcc -lgcc_eh -lc -lgcc -lgcc_eh
Compilation succeeded

// teste.c BEGIN
#include <mono/jit/jit.h>
#include <mono/metadata/environment.h>

 * Very simple mono embedding example.
 * Compile with: 
 * 	gcc -o teste teste.c `pkg-config --cflags --libs mono` -lm
 * 	mcs test.cs
 * Run with:
 * 	./teste test.exe

typedef struct
	MonoDomain *domain;
	const char *file;
	int argc;
	char **argv;
} MainThreadArgs;

static void main_thread_handler (gpointer user_data)
	MainThreadArgs *main_args=(MainThreadArgs *)user_data;
	MonoAssembly *assembly;

	assembly = mono_domain_assembly_open (main_args->domain,
	if (!assembly)
		exit (2);
	 * mono_jit_exec() will run the Main() method in the assembly.
	 * The return value needs to be looked up from
	 * System.Environment.ExitCode.
	mono_jit_exec (main_args->domain, assembly, main_args->argc,

main(int argc, char* argv[]) {
	MonoDomain *master_domain;
	MonoDomain *secondary_domain;
	const char *file;
	int retval;
	MainThreadArgs main_args;
	if (argc < 2){
		fprintf (stderr, "Please provide an assembly to load");
		return 1;
	file = argv [1];
	 * mono_jit_init() creates a domain: each assembly is
	 * loaded and run in a MonoDomain.
	master_domain = mono_jit_init (file);

	secondary_domain = mono_domain_create ();

#if 0
  // works
  // works

#if 1
  // works
	mono_runtime_exec_managed_code (master_domain,
  // fails
	mono_runtime_exec_managed_code (secondary_domain,


	retval=mono_environment_exitcode_get ();
  //mono_jit_cleanup (secondary_domain);
	mono_jit_cleanup (master_domain);
	return retval;
// teste.c END

// test.cs BEGIN
using System;

class Test {
	static void Main() {
// test.cs END

More information about the Mono-devel-list mailing list