Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Threads.. what to do?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Threads.. what to do?

    Hi People.
    I have been reading up on Threads in the win32 API help
    file but I am still a little unsure about when to use a
    "CriticalSection".
    I have an app that sits around listening to a spcecific
    port, when it gets a valid connection it spawns a new
    thread of the "wroker thread". as follows..

    Function WorkerThread&()
    CALL sub1 'recieve a file
    CALL sub2 'unpack the file
    CALL sub3 'process the file
    CALL sub4 'pack any outwards files for user
    CALL sub5 'send outwards file(s)
    Close thread
    End Function

    Are the subs/functions called from the thred that is created
    protected or do I nee to use "CriticalSection" before and
    after each call ?
    Also are there any guess-timates on how many connections I
    could recieve and process like this ?

    any help appreciated.

    N.

    ------------------
    I forgot to mention that the subs called will also
    call other subs/functions.




    [This message has been edited by Neil Hosgood (edited January 04, 2001).]

  • #2
    Covering all aspects of multi-threading is not something that can be covered in a message on this BBS - whole books have been written on the subject! However, it you can beg, borrow or buy a copy of Rector/Newcomers "Win32 Programming" then they have an excellent section on multi-threaded programming.

    Essentially, a critical section or some other form of synchronization (mutex, semaphore, etc) is required when a thread accesses memory that can be altered by another thread at the same moment. This covers GLOBAL and STATIC variables or any other block of memory. Local variables are automatically thread-safe as they are (generally speaking) stored on the stack allocated to each thread.

    For example, the following simple thread function is NOT thread-safe, since more than once thread can read and update the variable at any moment and corrupt the value in the variable if a context-switch (thread switch) occurs before all bytes of the target variable are successfully written by a given thread. Image that the thread that is writing a new valu to th variable is switched aftr only 2 bytes of the variable have has their new values written to memory. If another thread tries to read the value during the switch, it will read part of the new value and part of the old value!
    Code:
    FUNCTION WorkerThread(BYVAL x AS LONG) AS LONG
      DIM Total AS GLOBAL CUX   ' global variable!
      TOTAL = TOTAL * x / [email protected]@
    END FUNCTION
    However, by wrapping a critical section object around the line of code that updates the GLOBAL variable, your thread can be made thread-safe, since the critical section will only allow one thread to utilize the variable at any given moment.

    BTW, you'll note that a thread function MUST receive a 32-bit LONG or DWORD and it must be BYVAL... your code omits this parameter.

    Also, it looks like the functions your thread calls are using some form of GLOBAL variable storage (since you are not passing any parameters), so I would hazard a guess to say your code is far from thread-safe as it stands.

    In summary - get a copy of Rector/Newcomer - consider it an investment in yourself!

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment

    Working...
    X