Re: windows threads work!!

new topic     » goto parent     » topic index » view thread      » older message » newer message

jacques deschĂȘnes wrote:
> 
> 
> In the past I tested threads in euphoria without any success. At my first try
> and didn't insist because At that time i thought it was a limitation of
> euphoria
> interpreter.
> But this morning reading "windows internals" I learned that windows create a
> new stack for each thread a process create. if so multithread should in
> euphoria!
> I decided to revisit it and IT WORKED!!
> 
> }}}
<eucode>
> -- testing windows system threads in euphoria
> 
> without warning
> 
> 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
>  
>   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)
>   
> 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
> 
> 
> object fnVal
> atom counter, spinlock
> 
> counter = 0
> spinlock = 0
> 
> 
> function IncCounter(atom param)-- the thread function
> while 1 do
>   if spinlock = 1 then
>     counter +=1
>     if counter = 10 then spinlock = 2 return 0 end if
>     spinlock = 0
>   else
>     --sleep(500)  this does not work
>     Sleep(500)
>   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\n", hThread)
> if not hThread then  puts(1, "echec creation thread\n") abort(0) end if
> 
> while spinlock < 2 do
>  if not spinlock then
>    ? counter
>    spinlock = 1 
>  end if
> end while
> 
> fnVal = CloseHandle(hThread)
> </eucode>
{{{

> 
> Regards,
> Jacques DeschĂȘnes

Hi Jacques,

I tried using threads a long time ago too with Euphoria without 
success, so i decided to try you new version here, but it didnt
work for me (WinXP SP1).  I got an error:
 "EXW had a problem and had to close".
Any ideas what might be wrong?
Also, what op sys are you using, 95,98,2000, or XP ?


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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu