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:
parent
4b0899528b
commit
5d146ae426
1 changed files with 30 additions and 6 deletions
|
|
@ -8,22 +8,40 @@
|
|||
#endif
|
||||
|
||||
%{
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Foo;
|
||||
extern "C" void* working(void* t);
|
||||
extern "C" {
|
||||
void* SWIGSTDCALL working(void* t);
|
||||
}
|
||||
%}
|
||||
|
||||
%director Foo;
|
||||
|
||||
%inline {
|
||||
static void MilliSecondSleep(int milliseconds) {
|
||||
#ifdef _WIN32
|
||||
Sleep(milliseconds);
|
||||
#else
|
||||
usleep(milliseconds*1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
class Foo {
|
||||
public:
|
||||
int val;
|
||||
pthread_t *t;
|
||||
unsigned int thread_id;
|
||||
|
||||
Foo() : val(0) {
|
||||
Foo() : val(0), thread_id(0) {
|
||||
t = new pthread_t;
|
||||
}
|
||||
|
||||
|
|
@ -32,8 +50,12 @@ extern "C" void* working(void* t);
|
|||
}
|
||||
|
||||
void run() {
|
||||
#ifdef _WIN32
|
||||
_beginthreadex(NULL,0,run,this,0,&thread_id);
|
||||
#else
|
||||
pthread_create(t,NULL,working,this);
|
||||
sleep(5);
|
||||
#endif
|
||||
MilliSecondSleep(5000);
|
||||
}
|
||||
|
||||
virtual void do_foo() {
|
||||
|
|
@ -42,13 +64,15 @@ extern "C" void* working(void* t);
|
|||
};
|
||||
}
|
||||
|
||||
%inline {
|
||||
extern "C" void* working(void* t) {
|
||||
%inline %{
|
||||
extern "C" {
|
||||
void* working(void* t) {
|
||||
Foo* f = static_cast<Foo*>(t);
|
||||
while (1) {
|
||||
sleep(1);
|
||||
MilliSecondSleep(1000);
|
||||
f->do_foo();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
%}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue