Sharing Data Between Threads

Hello,

I have a mutithreaded application that has 2 threads.

Thread 1 is for obtaining the data, this has multiple sources. (this writes the data)
Thread 2 is for serving the data on a boost asio http connection. (this reads the data)

I planned on using a mutex to lock the data when writing to it so I dont get any dead lock. My question is, how do I share data between the two threads? Is there some kind of static/namespace trick that I can use to do this?

This is the basic structure of the http listener which can also be found here
https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/example/cpp03/http/server/
1
2
3
4
5
6
main.cpp
  |--server
       |--connection
            |--reply
            |--request_handler
                 |--request_parser


Thanks in advance for any help you can give





I tried to do it this way but I am not sure this is the best approach

// CSharedMemory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <mutex>

using namespace std;

class CSharedMemory
{
    public:
        CSharedMemory();
        virtual ~CSharedMemory();

        string GetTestText();

    protected:

    private:

        string m_sTest = "";
};

#endif // CSHAREDMEMORY_H 


// CSharedMemory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "CSharedMemory.h"

CSharedMemory::CSharedMemory()
{
    //ctor
}

CSharedMemory::~CSharedMemory()
{
    //dtor
}

string CSharedMemory::GetTestText()
{
    return "test worked";
}




//main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(int argc, char* argv[])
{
    CSharedMemory m_oSharedMemory;
    try
    {
        string sIpAddress = "MY_IP";
        string sPort = "MY_PORT";
        cout << "Listening: http://" << sIpAddress << ":" << sPort << endl;
        http::server::server s(sIpAddress, sPort, m_oSharedMemory);
        s.run();
    }
    catch (std::exception& e)
    {
      std::cerr << "exception: " << e.what() << "\n";
    }
}



// server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <boost/asio.hpp>
#include <string>
#include <boost/noncopyable.hpp>
#include "connection.h"
#include "connection_manager.h"
#include "request_handler.h"
#include "CSharedMemory.h"

namespace http {
namespace server {

/// The top-level class of the HTTP server.
class server
  : private boost::noncopyable
{
public:
  /// Construct the server to listen on the specified TCP address and port, and
  /// serve up files from the given directory.
  explicit server(const std::string& address, const std::string& port, CSharedMemory &sharedMemory);

  /// Run the server's io_context loop.
  void run();

private:
  /// Initiate an asynchronous accept operation.
  void start_accept();

  /// Handle completion of an asynchronous accept operation.
  void handle_accept(const boost::system::error_code& e);

  /// Handle a request to stop the server.
  void handle_stop();

  /// The io_context used to perform asynchronous operations.
  boost::asio::io_context io_context_;

  /// The signal_set is used to register for process termination notifications.
  boost::asio::signal_set signals_;

  /// Acceptor used to listen for incoming connections.
  boost::asio::ip::tcp::acceptor acceptor_;

  /// The connection manager which owns all live connections.
  connection_manager connection_manager_;

  /// The next connection to be accepted.
  connection_ptr new_connection_;

  /// The handler for all incoming requests.
  request_handler request_handler_;

  CSharedMemory m_oSharedMemory;
};

} // namespace server
} // namespace http

#endif // HTTP_SERVER_HPP 


// server.cpp partial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
server::server(const std::string& address, const std::string& port, CSharedMemory &sharedMemory)
  : io_context_(),
    signals_(io_context_),
    acceptor_(io_context_),
    connection_manager_(),
    new_connection_(),
    request_handler_()
{

    m_oSharedMemory = sharedMemory;

  signals_.add(SIGINT);
  signals_.add(SIGTERM);
#if defined(SIGQUIT)
  signals_.add(SIGQUIT);
#endif // defined(SIGQUIT)
  signals_.async_wait(boost::bind(&server::handle_stop, this));

  // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
  boost::asio::ip::tcp::resolver resolver(io_context_);
  boost::asio::ip::tcp::endpoint endpoint =
    *resolver.resolve(address, port).begin();
  acceptor_.open(endpoint.protocol());
  acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  acceptor_.bind(endpoint);
  acceptor_.listen();

  start_accept();
}


Can I keep passing the reference to the single instance of CSharedMemory like this to each class?
Last edited on
Topic archived. No new replies allowed.