Hey guys,
So, I got the ruby extension process to work, but I seem to be running into one little catch... multithreading.
FYI: I'm using Pelle's C with the options TBD suggested in his SUExt example. I'm on a Win 7 (64 bit) system (though hopefully that won't matter).
I'm trying to use _beginthread, but using TBD's settings, it seems I have an unresolved symbol '_beginthread'.
Just to let you know, I am kind of new to multithreading and DLLs. I have created threaded applications, but never a threaded dll.
Taking an example program that I have gotten to compile and run as a standalone app (pelle's C option multithreaded (lib)), I add the libs, includes and functions required to make it a ruby extension and it suddenly can't resolve _beginthread.
A couple of modifications to TBD's project settings and I get it to compile and even be recognized by the irb. However, it seems the second the thread tries to run, the whole irb closes.
The settings I modified to "get it to work" was:
Runtime library from multithread (dll) to multithread (lib)
and turn off "Omit default library in object files"
Any ideas?
Thanks,
Reg
// general Windows
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "advapi32.lib")
// Ruby
#define HAVE_ISINF 1 // isinf macro redefinition workaround
#pragma comment(lib, "msvcrt-ruby18.lib");
// DLL
#pragma comment(lib, "crt.lib");
#include <stdio.h>
#include <windows.h>
#include <process.h> // needed for _beginthread()
// Ruby headers
#include "ruby.h"
void silly( void * ); // function prototype
// De fining a space for information and references about the module to be stored internally
VALUE KMRH = Qnil;
int j = -1;
// our function
VALUE method_display_model(VALUE self, VALUE input)
{
// Our program's first thread starts in the main function.
printf( "Now in the main function.\n" );
// Let's now create our second thread and ask it to start
// in the silly() function.
// _beginthread( silly, 0, (void*)12 ); //Commented out to see if the program would stay alive and it does
// From here on there are two separate threads executing
// our one program.
// This main thread can call the silly() function if it wants to.
while(j != 0)
{
silly( (void*)j );
j--;
}
Sleep( 10000 );
int i = NUM2INT(input);
return INT2NUM(i + 1);
}
// The initialization method for this module
void Init_KMRH(void)
{
KMRH = rb_define_class("KMRH", rb_cObject);
rb_define_method(KMRH, "display", method_display_model, 1);
}
void silly( void *arg )
{
int k = 10;
while(k-- != 0)
{
// printf( "The silly() function was passed %d\n", (INT_PTR)arg );
printf( "The silly() function was passed %d\n", k );
}
}