Author Topic: Threads and Classes  (Read 2515 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Threads and Classes
« on: June 18, 2012, 09:58:20 AM »
If classes contain static members then, as a general rule, they cannot be used by more than one thread at a time. The simplest solution is to create separate classes for each thread.

In this scheme, each set of classes is recreated within each thread. I have put them inside the thread functions, (though it is not essential to do this; they could be placed immediately before each thread function).

Code: OxygenBasic
  1.  
  2. 'CLASSES WITH STATIC MEMBERS:
  3. 'HOW TO DEPLOY FOR MULTITHREADING
  4.  
  5. macro InclStatClasses()
  6.   '
  7.  class CA
  8.     static sys s
  9.     sys a
  10.     method m(sys n)
  11.       print "CA ok: " n
  12.     end method
  13.   end class
  14.   '
  15.  class CB
  16.     static sys s
  17.     sys a
  18.     method m(sys n)
  19.       print "CB ok: " n
  20.     end method
  21.   end class
  22.   '
  23. end macro
  24.  
  25. function thread1(sys n) external
  26.   InclStatClasses
  27.   CA c
  28.   c.m n
  29.   #recordof CA
  30. end function
  31.  
  32. function thread2(sys n) external
  33.   InclStatClasses
  34.   CA c
  35.   c.m n
  36.   #recordof CA
  37. end function
  38.  
  39. 'CHECK EACH THREAD ALONE
  40.  
  41. thread1 1
  42. thread2 2
  43.  
  44.  

Charles

kryton9

  • Guest
Re: Threads and Classes
« Reply #1 on: June 18, 2012, 11:43:02 AM »
Thanks Charles, can't wait to test later tonight and get used to using threads. I need to feel comfortable with them if I am serious about a game engine.

Charles Pegge

  • Guest
Re: Threads and Classes
« Reply #2 on: June 18, 2012, 12:47:14 PM »
The next aspect is to establish a simple communication sytem so that threads can communicate with the controller and the other threads.

In this example,  a table is used to send and receive integer messages. There is a separate cell for each communication, so there is no possibility of conflicting signals.

One point to note is that the '%' equates are expanded late.

Code: OxygenBasic
  1.  
  2. 'CALLS FOR MULTITHREADING ETC
  3.  
  4. library "kernel32.dll"
  5. sys   CreateThread (sys lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, *lpThreadId)
  6. dword WaitForMultipleObjects(sys nCount,*lpHandles, bWaitAll, dwMilliseconds)
  7. bool  CloseHandle(sys hObject)
  8. void  Sleep(sys dwMilliSeconds)
  9. library ""
  10.  
  11. % INFINITE 0xFFFFFFFF  'Infinite timeout
  12.  
  13.  
  14. 'COMMUNICATION SYSTEM
  15. '====================
  16.  
  17. indexbase 1
  18.  
  19. % Threads          16 'including controller
  20. % MsgNone          0
  21. % MsgSlow          1
  22. % MsgFast          2
  23. % MsgHello         42
  24. % MsgTerminate     255
  25. '
  26. % MsgFromCtrler    n*threads
  27. % MsgFromA         n*threads+1
  28. % MsgFromB         n*threads+2
  29. '...
  30. % MsgToCtrler      n
  31. % MsgToA           1*threads+n
  32. % MsgToB           2*threads+n
  33. '...
  34.  
  35.  
  36. sys signal[threads*threads] 'communications table
  37.  
  38.  
  39. 'THREAD FUNCTIONS
  40.  
  41. function threadA(n) external
  42. finit
  43. do
  44.   if signal[MsgFromCtrler]=MsgTerminate then exit do
  45.   signal[MsgToCtrler]=MsgHello
  46.   sleep 1
  47. end do
  48. print "A terminated " n
  49. end function
  50.  
  51. function threadB(n) external
  52. finit
  53. do
  54.   if signal[MsgFromCtrler]=MsgTerminate then exit do
  55.   signal[MsgToCtrler]=MsgHello
  56.   sleep 1
  57. end do
  58. print "B terminated " n
  59. end function
  60.  
  61. sys Funs[threads]={@threadA,@threadB}, ThreadsRunning=2
  62.  
  63. 'START THREADS
  64.  
  65. sys  hThread[threads], id[threads], i, n
  66. '
  67. for i=1 to threadsRunning
  68.   hThread[ i ] =  CreateThread 0,0,funs[ i ],i,0,id[ i ]
  69. next
  70.  
  71.  
  72. 'CHECK FOR HELLO MESSAGES
  73.  
  74. sleep 10 'wait for threads to activate
  75.  
  76. print "Running: " signal[MsgFromA] "," signal[MsgFromB]
  77.  
  78.  
  79. 'END THREADS
  80.  
  81. signal[MsgToA]=MsgTerminate
  82. signal[MsgToB]=MsgTerminate
  83.  
  84. WaitForMultipleObjects ThreadsRunning, hThread, 1, INFINITE
  85.  
  86. for i=1 to ThreadsRunning
  87.   CloseHandle hThread[ i ]
  88. next
  89.  
  90.  

Charles