Home

Add

Edit

Without Linenumbers

Code in Textfield

Download

  1. #IF Defined(__FB_LINUX__)
    
  2.     Declare Function pthread_cancel Cdecl Lib "c" Alias "pthread_cancel" (Byval pthread_t As Integer) As Integer
    
  3. #ELSEIF Defined(__FB_WIN32__)
    
  4.     #INCLUDE Once "windows.bi"
    
  5. #ENDIF
    
  6. 
    
  7. 
    
  8. 
    
  9. Sub ThreadCancel(thread As Any Ptr)
    
  10. #IF Defined(__FB_LINUX__)
    
  11.     pthread_cancel(Cast(Integer Ptr, thread)[0])
    
  12. #ELSEIF Defined(__FB_WIN32__)
    
  13.     TerminateThread(Cast(Handle Ptr, thread)[0], 0)
    
  14. #ENDIF
    
  15. End Sub
    
  16. 
    
  17. 
    
  18. 
    
  19. Sub threadtest()
    
  20. Print "thread-running"
    
  21. Dim C As Uinteger
    
  22. Do
    
  23.     Print Timer()
    
  24.     Sleep 500, 1
    
  25.     c += 1
    
  26.     If c = 10 Then Exit Do
    
  27. Loop
    
  28. Print "thread-exiting loop exit"
    
  29. End Sub
    
  30. 
    
  31. 
    
  32. 
    
  33. Print "init thread..."
    
  34. Dim tthread As Any Ptr = Threadcreate(Cast(Any Ptr, @threadtest))
    
  35. Print "wait 2 secs"
    
  36. Sleep 2000, 1
    
  37. Print "try cancle thread..."
    
  38. ThreadCancel(tthread)
    
  39. Print "wait threadexit..."
    
  40. Threadwait(tthread)
    
  41. Print "thread exit success!"
    
  42. End 0
    
  43. 
    
  44.