Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:11:40+00:00 2026-05-28T01:11:40+00:00

Here is my attempt at implementing a C++ event. class Event{ typedef std::tr1::function<void( int&

  • 0

Here is my attempt at implementing a C++ event.

class Event{
    typedef std::tr1::function<void( int& )> CallbackFunction;
    std::list< CallbackFunction > m_handlers;

    template<class M>
    void AddHandler(M& thisPtr, void typename (M::*callback)(int&)) 
    {           
        CallbackFunction bound = std::tr1::bind(callback, &thisPtr, _1);
        m_handlers.push_back(bound);
    }

    void operator()(int& eventArg) 
    { 
        iterate over list...
        (*iter)(eventArg);

    }}

The trouble here is thread-safety. If AddHandler and operator() are called at the same time things could break.

What is the best way to sync this? Using a mutex could kill performance. I wonder what happens behind the scenes of boost::signals or C# event in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:11:40+00:00Added an answer on May 28, 2026 at 1:11 am

    A mutex is definitely what you’re looking for. If each Event has its own mutex, I wouldn’t worry much about performance; the reason is that unless you’re adding a lot of handlers during the time you’re handling events, it’s unlikely the mutex will be in contention and slow you down.

    However, if you have more than one thread calling the operator() method on the same object, this mutex could be a problem. But without it, how will you ensure your callbacks are invoked in a thread-safe way anyhow? (I notice you’re passing in an integer reference and returning void, so I’m assuming these are not reentrant handlers.)

    EDIT: very good question in your comment. To be honest, I never put much thought into whether or not mutexes had much overhead when used in a synchronous way. So I put together this little test.

    
    #include <stdio.h>
    #include <pthread.h>
    
    #define USE_PTHREAD_MUTEX 1
    
    int main(int argc, char * argv[]) {
    
    pthread_mutex_t mutex;
    pthread_mutex_init(&mutex, NULL);
    
    long useless_number = 0;
    long counter;
    
      for(counter = 0; counter < 100000000; counter++) {
        #if USE_PTHREAD_MUTEX
        pthread_mutex_lock(&mutex);
        #endif
        useless_number += rand();
    
        #if USE_PTHREAD_MUTEX
        pthread_mutex_unlock(&mutex);
        #endif
      }
    
      printf("%ld\n", useless_number);
    
    }
    
    

    I ran this on my system and got the following runtimes.

    With USE_PTHREAD_MUTEX 0, the average runtime is 1.2 seconds.

    With USE_PTHREAD_MUTEX 1, the average runtime is 2.8 seconds.

    Thus, to answer your question, there is definitely overhead. Your mileage may vary. Besides, if multiple threads are competing for access to a resource, more time will be spent blocking, necessarily. Also, in a purely synchronous context, it’s likely that more time will be spent accessing the shared resource than waiting for a mutex to lock/unlock. That is to say, the overhead of the mutex logic itself will probably be insignificant compared to these things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently implementing a copy constructor for a Linked List class. When I create
How to dynamically change the timeout of the cycle plugin here is my attempt
I'm trying to calculate time blocks from given date range. Here's my attempt: <?php
Here is my first attempt at validating XML with XSD. The XML file to
Here's the flow I'm looking for for authentication: Attempt to pull in the user's
Here in stackoverflow, if you started to make changes then you attempt to navigate
I'm getting this error: -[NSCFArray insertObject:atIndex:]: attempt to insert nil Here is the .m
I'm after a simple stored procedure to drop tables. Here's my first attempt: CREATE
After my last, failed, attempt at asking a question here I'm trying a more
Here is my attempt which is NOT tail call optimized because I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.