CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2019
    Posts
    8

    Sharing Data between Threads

    Hi

    I have several objects that sit on threads. I would like to shared data between classes.

    I have two threads that I want to interact if possible. I have a Message Manager class and a Console class. I want each to sit on their own thread. My problem is this. Suppose I have input in the Console class which is ok, because its thread has a loop that basically sits there waiting for input. Say I get some input like D_PAD_DOWN. I want to be able to send a message to the message manager saying D_PAD_DOWN occurred. Then Message Manager goes off and does its own thing. Message Manager has its own loop as well that adds messages to a queue and dispatches the messages to a Scheduler.

  2. #2
    Join Date
    Aug 2020
    Posts
    2

    Re: Sharing Data between Threads

    Hello,

    I hope this isnfo is useful to u,

    multi-thread programs are very difficult to write correctly and debug. The most useful advice is just to "avoid it", manage threads at a higher level, and reduce the places where threading issues can hide.
    It is also extremely useful to reduce at a minimum the sharing of mutable data (since immutable data is race-free). Therefore another reason to avoid non-constant globals, and prefer to copy the data between threads, even if it might be a little more expensive.

    But from time to time, there might be some need to share something mutable, and it’s important to see what tools and techniques we have at our disposal.

    The most known and used tool for ensuring consistency between threads when sharing mutable data is a mutex (std::mutex, std::timed_mutex, std::recursive_mutex, std::recursive_timed_mutex in C++11).
    Last edited by 2kaud; September 2nd, 2020 at 06:26 AM. Reason: Advertising link removed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured