When Should I Have an Event for Each Thread? - C++

Posted on 2009-04-16
Last Modified: 2012-05-06
1. When would be appropriate to use an event for each thread?

2. Mutex should be used when two threads are reading/writing to a shared resource, right? Is there an alternative - just curious to know?

Question by:F-J-K
    LVL 19

    Expert Comment

    A summary of thread syncronization objects:

    LVL 39

    Assisted Solution

    1. When you need to signal the thread externally from another thread.

    2. Yes a mutex (mutual exclusion) object is used to sync events. You can also use a CriticalSection on Windows, which is lighter weight because unlike a mutex it doesn't generally require transition to kernel mode (a special mode where the kernel is on control).

    If you just want to do simple things such as increment or change a value Windows provides a set of simple Interlock functions to do this

    On linux you can use a semaphore; however, mutexes are generally just specializations of semaphore to give them mutual exclusion semantics.

    It's actually possible to provide mutex semantics without any special objects; however, the code to provide the required interlocking is very complex and beyond the scope of this thread.
    LVL 39

    Accepted Solution

    >>>> When would be appropriate to use an event for each thread?

    I am using events before mutexes if I need a one-time synchronisation between threads. Here an event IMO is more straight-forward. Assume you have local data in the main thread and you want to pass a pointer to the local data  to a worker thread. Then, the main thread has to wait for the worker thread to start and copy the data before it could delete the local data. For that scenario a mutex is not appropriate cause the main thread need to wait for the worker thread which isn't yet active, hence it couldn't set the lock the main thread could wait for.

    >>>> Is there an alternative - just curious to know?
    you can give each thread its own piece of the shared resource where it is has exclusive right to write on. The other thread() would only read from released data of that part. Think of a message queue which contains 10 released messages. The worker thread can add a new message without problem and increases the message counter when that was done. Any reader would read only up to the current message counter. The problem with such concepts is that they can't easily made safe if you want delete messages from the queue. If there is only one reader, the reader could delete messages after read but may not update any container attribute which was necessary for the writing part of the container. Nevertheless these concepts were very difficult and using a mutex for exclusive access to a container is much easier.

    >>>>It's actually possible to provide mutex semantics without any special objects;

    In Windows you could use the InterlockedIncrement which provides an atomar incrementing of a shared (global) integer variable.  Atomar means that while the integer was incremented there is no interrupt so that two threads both could increment nearly same time but only one increment actually was done.

    The global integer was initialized with zero waht means no lock. The mutex lock for an exclusive code part then looks like

      while (InterlockedIncrement(&global_lock) > 1)
            // if coming here a second thread has done the same
            // we make our own increment reverse and try again
            Sleep(10);   // wait 10 milliseconds
      // Coming here we are exclusive cause the global integer is 1

      // do something exclusive

      // release lock after use
    LVL 1

    Author Closing Comment

    Well done. I'm pretty new to multi-threading. It does not seem an easy thing to learn, it can get so complex as far as i see.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    Help with my python script 6 135
    scoreUp challenge 14 40
    powerN  challenge 3 34
    changeXy challenge 13 40
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now