Should I Use A Critical Section?

This is a discussion on Should I Use A Critical Section? within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I've never written a multithreaded application that shares variables across threads but now I've been asked to write a ...

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,263

    Question Should I Use A Critical Section?

    Hello,

    I've never written a multithreaded application that shares variables across threads but now I've been asked to write a server control application and have found my efforts being thwarted at every turn by mysterious timing-specific bugs which I've traced to threads trying to work with one memory location all at once.

    Specifically, I have a singly linked list that describes the clients connected to the server. Each of these client structs also has a singly linked list containing the parameters of the client.

    I also have at least three threads. There is the GUI thread, responsible for pumping messages. There is the server thread, which verifies client connections and creates worker threads. And there is one or more worker threads (one thread per client).

    The GUI thread does not modify the client list, but it does read it and the parameters of each client in order to display them in a window.

    The server thread adds and removes clients from the client list. When signalled to terminate, it signals each worker thread and gives them three seconds to terminate gracefully. If by then they are still running they are killed and the client's parameter list is freed by the server.

    The worker thread adds to, removes from and modifies the client parameters list.

    To combat the problems that I'm encountering, I've given thought to creating a critical section object and getting each thread to enter it whenever they want to do anything involving the client list or any of the parameter lists. While any one thread is reading or modifying the lists the other threads will have to wait.

    But... should this one CS object apply to both the client list and all the parameter lists, or should I add a CS object handle to the client's struct and have a CS object per client for the parameter list?

    Safe multithreading is sooo tricky...

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Anytime you have multiple threads accessing the same data you will need either a mutex or a critical section. I highly recommend using critical sections since they are both easy and fast.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Critical sections and mutex's have basically the same function, except that mutex's are used for interprocess synchonrization, while critical sections are used for intraprocess (interthread) synchronization. Since you are working with threads in a single process, critical sections are the correct choice. Be aware though that using a critical section effectively serializes access to the linked list, so try to keep the time a thread is in the critical section to a minimum.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maths For Game Programming?
    By kolliash in forum Game Programming
    Replies: 13
    Last Post: 09-18-2008, 11:53 AM
  2. In need of guidance
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 01:43 PM
  3. Threads, Linked Lists, Semaphores, Critical Section ...
    By _jr in forum Windows Programming
    Replies: 4
    Last Post: 06-21-2006, 08:14 AM
  4. STL thread safety
    By Hunter2 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2004, 08:09 PM
  5. Section Finder
    By Soul.India in forum C Programming
    Replies: 6
    Last Post: 11-23-2003, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21