Ported threading to win32

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9247 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2006-08-11 22:55:19 +00:00
commit 5d146ae426

View file

@ -8,22 +8,40 @@
#endif #endif
%{ %{
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#else
#include <pthread.h> #include <pthread.h>
#include <unistd.h>
#endif
#include <iostream> #include <iostream>
class Foo; class Foo;
extern "C" void* working(void* t); extern "C" {
void* SWIGSTDCALL working(void* t);
}
%} %}
%director Foo; %director Foo;
%inline { %inline {
static void MilliSecondSleep(int milliseconds) {
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep(milliseconds*1000);
#endif
}
class Foo { class Foo {
public: public:
int val; int val;
pthread_t *t; pthread_t *t;
unsigned int thread_id;
Foo() : val(0) { Foo() : val(0), thread_id(0) {
t = new pthread_t; t = new pthread_t;
} }
@ -32,8 +50,12 @@ extern "C" void* working(void* t);
} }
void run() { void run() {
#ifdef _WIN32
_beginthreadex(NULL,0,run,this,0,&thread_id);
#else
pthread_create(t,NULL,working,this); pthread_create(t,NULL,working,this);
sleep(5); #endif
MilliSecondSleep(5000);
} }
virtual void do_foo() { virtual void do_foo() {
@ -42,13 +64,15 @@ extern "C" void* working(void* t);
}; };
} }
%inline { %inline %{
extern "C" void* working(void* t) { extern "C" {
void* working(void* t) {
Foo* f = static_cast<Foo*>(t); Foo* f = static_cast<Foo*>(t);
while (1) { while (1) {
sleep(1); MilliSecondSleep(1000);
f->do_foo(); f->do_foo();
} }
return 0; return 0;
} }
} }
%}