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

  • Home
  • SEARCH
  • 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 3274758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:04:23+00:00 2026-05-17T19:04:23+00:00

I have a UDP based application that is implemented using a thread pool. Messages

  • 0

I have a UDP based application that is implemented using a thread pool.

Messages are pushed onto a queue and the thread pool is woken when there are things to do or messages on the queue. The thread pool processes each of the messages and hands them off to
session objects which hold some state. i.e. the UDP packets are part of a session and the session is reassembling a large block of data.

That session object needs to access a shared resource. However, another session object can also read from the resource, but not while it’s being written to.

Now the problem is that the session objects DoWork method can be called from different threads and I need to prevent anyone writing to it. So to do that I need to put a lock around the resource.

Here is where the problem arises. If I use a standard mutex, it’s not portable between threads so I will try to access the resource and should be able to put the data into the resource but I can’t unless I’m the original thread that locked the resource.

It’s as thought I need a session key to gain access to the resource rather than my
thread id.

How do I get around this? boost shared_mutex here seems a bit limited 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-17T19:04:24+00:00Added an answer on May 17, 2026 at 7:04 pm

    What I’m doing is making the resource an object.
    A skeleton version looks
    like this:

    e.g.

    class Resource
    {
    public:
      enum {READ, WRITE};
    
      void Open(int mode=READ)
      {
         if (mode == WRITE){
           Lock();
           // Access resource
         } else if (mode == READ){
           // Try to get read access (scoped version)
            boost::shared_lock<boost::shared_mutex> read(lock_, boost::try_to_lock);
            if (!read){
                // throw exception
            }
    
            // Read access to resource
         }
      }
    
      Lock()
      {
        lock_.lock_upgrade();
        lock_.unlock_upgrade_and_lock();
      }
    
      Unlock()
      {
        lock_.unlock();
      }
    
    private:
      boost::shared_mutex lock_;
    }
    

    Now I can have multiple readers. When I want to write to the resource (at any time) I can call Lock(). Open does it too. And can call Unlock when done. A different thread can unlock than the one that locked it.

    Also, reads are prevented while writing, until unlock is called.

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

Sidebar

Related Questions

I have problem with missing messages when using nonblocking read in udp between two
Have you ever seen any of there error messages? -- SQL Server 2000 Could
I'm writing a networking application that uses ASIO/UDP to send and receive between a
I'm trying to write an application using the UDP module included in the inetmanet
I have a UDP socket that is bound to INADDR_ANY to listen to packets
I have been unable to receive UDP multicast under VxWorks 5.5. I've joined the
You have multiple network adapters. Bind a UDP socket to an local port, without
UDP doesnot sends any ack back, but will it send any response? I have
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I

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.