Set Correlation ID during Receive after MsgSend when using multi-threading

Blockquote
class Logic : public std::enable_shared_from_this {
public:
void OnReqEvent(const elf::core::DomEvent &event){
std::shared_ptr req_ptr = std::make_shared();
bool succeed = ReqReader::PopulateReq(
event.GetDocument(), *req_ptr);
auto req_copy = DeepCopyReq(*req_ptr);
auto self = shared_from_this();
std::thread req_thread(self, req_copy {
// Set a thread name for debugging
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (self && req_copy)
self->ProcessReq(req_copy);
});
req_thread.detach();
}

    void ProcessReq(const std::shared_ptr<Req> &req) {
          // Bunch of code to Process the request
          if(msg_svc)
            msg_svc->PopulateandSend(reply_params)
    }

    void PopulateandSend(const std::vector<std::string> &reply_params) {
        // Code to populate and send the message
        auto reply = std::make_shared<Reply>();
        //Populate the reply with data
        if(msg_svc) {
            msg_svc->Send(reply);
        }
    }

};

The above code uses solace events to receive messages sent through the broker. When the reply message is sent within the OnReqEvent or if req_thread.join() is used, there are no issues and the code runs smoothly. However, since the processing of the request takes a while, I tried to push it into a separate thread to keep the main event thread free to receive more incoming messages. Upon using req_thread.detach(), there is a segmentation fault pointing to an issue with setCorrrelationID. I’m at wits end trying to solve this. If anyone has experienced a similar problem, kindly help.

I’m using a C-API from solace and most of my code is in C++. Thanks in advance!

Hi there, I’m Solly AI, I generate responses to your questions using the power of the Solace Agent Mesh:

Please remember that AI technology can make mistakes so ensure that you read our Disclaimer.

AI Generated responses are a work in progress, your help in evaluating my response would be awesome! If my response makes sense, please mark it as a solution or give a :+1:. If my response doesn’t make sense for your topic, please react to it with a :shaking_face:.

Looking at the provided documentation, I can identify several key points about using the C API with threads:

  1. The C API uses a single Context thread for performing API-related functions like reading sockets, running timers, etc.

  2. When using message handlers/callbacks, they run on the API’s Context thread, not an application-owned thread.

  3. It is strongly discouraged to do significant work in callback handlers as it can block the Context thread and cause issues like disconnects due to keepalive timer expiration.

For your specific issue with segmentation faults when using setCorrrelationID with detached threads:

The segmentation faults are likely occurring because you are trying to access the Solace C API from detached threads that are not properly synchronized with the API’s Context thread. Since the API is not thread-safe for these operations, this can lead to memory corruption and crashes.

The recommended approach is to:

  1. Keep API operations on the main Context thread
  2. Use application-owned threads only for message processing
  3. Use thread-safe queues or data structures to pass messages between the API thread and processing threads
  4. Only ACK messages and perform other API operations from the Context thread

For more detailed information, I recommend reviewing:

The fact that it works when joining threads or using the main event thread supports this analysis, as those approaches maintain proper thread synchronization with the API Context.

Thank you for the answer. I plan to use thread-safe queues through mutex to push the messages to be sent onto the queue while API can deque them to send. Can you provide an example with C API for Use thread-safe queues or data structures to pass messages between the API thread and processing threads? Thanks.