windows threads work!!

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

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!!

-- 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)


Regards,
Jacques DeschĂȘnes

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

Search



Quick Links

User menu

Not signed in.

Misc Menu