1. threads further testing

I made more testing of threads in euphoria.
if I run my test code with exw.exe it crash.
if I run it with exwc.exe it run nice.
I tested writting to console using puts() an nothing is printed to screen but
not crash.
I replaced puts() by win32api function  WriteConsole() and it works fine.

temporary conclusions: 
1)if one avoid call to statically linked C run time functions from threads it
works.
2) exwc.exe  seem to be thread safe  but not exw.exe.
3) So one can write multi-threaded console apps in euphoria

platform tested on: windows xp sp2
euphoria interpreter:  exwc.exe version 2.5  

my last test code:
-- testing windows system threads in euphoria

without warning

include wildcard.e
include misc.e
include machine.e
include dll.e

constant kernel32=open_dll("kernel32.dll")

if kernel32 = -1 then 
  puts(1,"failed to open kernel32.dll\n")
  abort(1)
end if

constant 
  CREATE_SUSPENDED = 4,
  STD_OUTPUT_HANDLE = -11
  
constant
iCreateThread=define_c_func(kernel32,"CreateThread",{C_POINTER,C_UINT,C_POINTER,C_POINTER,C_UINT,C_POINTER},C_UINT),
  iSleep=define_c_proc(kernel32,"Sleep",{C_UINT}),
  iCloseHandle = define_c_func(kernel32,"CloseHandle",{C_UINT},C_UINT),
  iResumeThread = define_c_func(kernel32,"ResumeThread",{C_UINT},C_UINT),
  iGetStdHandle = define_c_func(kernel32,"GetStdHandle",{C_UINT},C_UINT),
iWriteConsole =
  define_c_func(kernel32,"WriteConsoleA",{C_UINT,C_POINTER,C_UINT,C_POINTER,C_POINTER},C_UINT)
  
global function CreateThread(atom lpThreadAttributes,
atom dwStackSize,  -- initial thread stack size, in
                            bytes
atom lpStartAddress,      -- pointer to thread
                            function
                            atom lpParameter, -- argument for new thread 
                            atom dwCreationFlags,      -- creation flags 
atom lpThreadId  -- pointer to returned thread
                            identifier
                            )
return
  c_func(iCreateThread,{lpThreadAttributes,dwStackSize,lpStartAddress,lpParameter,dwCreationFlags,lpThreadId})
end function --CreateThread()

global function CloseHandle(atom handle)
  return c_func(iCloseHandle,{handle})
end function -- CloseHandle()

global procedure Sleep(integer ms)
  c_proc(iSleep,{ms})
end procedure

global function GetStdHandle(atom nStdHandle)
  return c_func(iGetStdHandle,{nStdHandle})
end function

global procedure WriteConsole(atom hConsole, sequence text)
atom void, pText, pCharWritten
  pText = allocate_string(text)
  pCharWritten =allocate(4)
  void = c_func(iWriteConsole,{hConsole,pText,length(text),pCharWritten,0})
  free(pText)
  free(pCharWritten)
end procedure

object fnVal
atom counter, spinlock


function IncCounter(atom param)-- thread function  (increment couter global
variable)
atom stdout
stdout = GetStdHandle(STD_OUTPUT_HANDLE) -- handle not inherited, get it from
  parent process.
  WriteConsole(stdout,"writing to console from IncCounter thread\n")
while 1 do
  if spinlock = 1 then
    counter +=1
    if counter = 10 then spinlock = 2 return 0 end if -- thread termination
    spinlock = 0
  else
    --sleep(500)  this does not work propably call static linked crt function.
    Sleep(500)  win32api wrapper work
   
  end if
end while
end function

atom lpStartAddress   lpStartAddress = call_back(routine_id("IncCounter")) 
atom lpThreadId  lpThreadId = allocate(4)
atom lpParam   lpParam = allocate(4)  poke4(lpParam,0)
atom hThread

hThread = CreateThread(0,0,lpStartAddress,lpParam,0,lpThreadId)
printf(1,"thread handle %d\n", hThread)
if not hThread then  puts(1, "echec creation thread\n") abort(0) end if
counter = 0
spinlock = 0
while spinlock < 2 do
 if not spinlock then
   ? counter
   spinlock = 1 
 end if
end while
fnVal = CloseHandle(hThread)


regards,
Jacques Deschênes

new topic     » topic index » view message » categorize

2. Re: threads further testing

jacques deschênes wrote:
> 
> 
> I made more testing of threads in euphoria.
> if I run my test code with exw.exe it crash.
> if I run it with exwc.exe it run nice.
> I tested writting to console using puts() an nothing is printed to screen but
> not crash.
> I replaced puts() by win32api function  WriteConsole() and it works fine.
> 
> temporary conclusions: 
> 1)if one avoid call to statically linked C run time functions from threads it
> works.
> 2) exwc.exe  seem to be thread safe  but not exw.exe.
> 3) So one can write multi-threaded console apps in euphoria
> 
> platform tested on: windows xp sp2
> euphoria interpreter:  exwc.exe version 2.5  
> 
> my last test code:
> }}}
<eucode>
> -- testing windows system threads in euphoria
> 
> without warning
> 
> include wildcard.e
> include misc.e
> include machine.e
> include dll.e
> 
> constant kernel32=open_dll("kernel32.dll")
> 
> if kernel32 = -1 then 
>   puts(1,"failed to open kernel32.dll\n")
>   abort(1)
> end if
> 
> constant 
>   CREATE_SUSPENDED = 4,
>   STD_OUTPUT_HANDLE = -11
>   
> constant
>  
>   iCreateThread=define_c_func(kernel32,"CreateThread",{C_POINTER,C_UINT,C_POINTER,C_POINTER,C_UINT,C_POINTER},C_UINT),
>   iSleep=define_c_proc(kernel32,"Sleep",{C_UINT}),
>   iCloseHandle = define_c_func(kernel32,"CloseHandle",{C_UINT},C_UINT),
>   iResumeThread = define_c_func(kernel32,"ResumeThread",{C_UINT},C_UINT),
>   iGetStdHandle = define_c_func(kernel32,"GetStdHandle",{C_UINT},C_UINT),
>   iWriteConsole =
>   define_c_func(kernel32,"WriteConsoleA",{C_UINT,C_POINTER,C_UINT,C_POINTER,C_POINTER},C_UINT)
>   
> global function CreateThread(atom lpThreadAttributes,
>                             atom dwStackSize,  -- initial thread stack size,
>                             in bytes
>                             atom lpStartAddress,      -- pointer to thread
>                             function
>                             atom lpParameter, -- argument for new thread 
>                             atom dwCreationFlags,      -- creation flags 
>                             atom lpThreadId  -- pointer to returned thread
>                             identifier
>                             )
>   return
>   c_func(iCreateThread,{lpThreadAttributes,dwStackSize,lpStartAddress,lpParameter,dwCreationFlags,lpThreadId})
> end function --CreateThread()
> 
> global function CloseHandle(atom handle)
>   return c_func(iCloseHandle,{handle})
> end function -- CloseHandle()
> 
> global procedure Sleep(integer ms)
>   c_proc(iSleep,{ms})
> end procedure
> 
> global function GetStdHandle(atom nStdHandle)
>   return c_func(iGetStdHandle,{nStdHandle})
> end function
> 
> global procedure WriteConsole(atom hConsole, sequence text)
> atom void, pText, pCharWritten
>   pText = allocate_string(text)
>   pCharWritten =allocate(4)
>   void = c_func(iWriteConsole,{hConsole,pText,length(text),pCharWritten,0})
>   free(pText)
>   free(pCharWritten)
> end procedure
> 
> object fnVal
> atom counter, spinlock
> 
> 
> function IncCounter(atom param)-- thread function  (increment couter global
> variable)
> atom stdout
>   stdout = GetStdHandle(STD_OUTPUT_HANDLE) -- handle not inherited, get it
>   from parent process.
>   WriteConsole(stdout,"writing to console from IncCounter thread\n")
> while 1 do
>   if spinlock = 1 then
>     counter +=1
>     if counter = 10 then spinlock = 2 return 0 end if -- thread termination
>     spinlock = 0
>   else
>     --sleep(500)  this does not work propably call static linked crt function.
>     Sleep(500)  win32api wrapper work
>    
>   end if
> end while
> end function
> 
> atom lpStartAddress   lpStartAddress = call_back(routine_id("IncCounter")) 
> atom lpThreadId  lpThreadId = allocate(4)
> atom lpParam   lpParam = allocate(4)  poke4(lpParam,0)
> atom hThread
> 
> hThread = CreateThread(0,0,lpStartAddress,lpParam,0,lpThreadId)
> printf(1,"thread handle %d\n", hThread)
> if not hThread then  puts(1, "echec creation thread\n") abort(0) end if
> counter = 0
> spinlock = 0
> while spinlock < 2 do
>  if not spinlock then
>    ? counter
>    spinlock = 1 
>  end if
> end while
> fnVal = CloseHandle(hThread)
> </eucode>
{{{

> 
> regards,
> Jacques Deschênes

Hi again Jacques,

I tried again using exwc.exe and it ran ok one time out of about
6 or 7 runs so i gave up.  That one time it did run however...

When i build C code i have to specify to the compiler that it
needs to be built with the multi threaded libraries.  I think
Rob would have to do that with exw or exwc also.
It even says somewhere in the Windows doc's that in order to use
the thread functions your program must have been built with
the multi threaded option.
I have no idea why it can run one time out of 6 and not the
other 5 times however.



Take care,
Al

E boa sorte com sua programacao Euphoria!


My bumper sticker: "I brake for LED's"

 From "Black Knight":
"I can live with losing the good fight,
 but i can not live without fighting it".
"Well on second thought, maybe not."

new topic     » goto parent     » topic index » view message » categorize

3. Re: threads further testing

Hi Al,
Didn't crash yet on my PC. I'll do more testing and reading.
Thanks for testing it.

regards,
Jacques Deschênes

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu