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 6960575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:25:09+00:00 2026-05-27T15:25:09+00:00

Title sort of says it all, I thought storing an object in an object

  • 0

Title sort of says it all, I thought storing an object in an object container would allow for easy cross-threaded class-member access, because it essentially stores the object in memory space that is managed by the object container, in this case a map. Is this incorrect? Because the following is happening;

Client class:

class Client
{
public:
    Client(std::string clientID,SOCKET sock,bool quit);
    boost::thread_group *group;
    /*CUT*/
    std::string clientID;
    std::deque<std::string> snapShotsQueue;
    SOCKET sock;
    bool quit;
    void sendMessage(std::string);
    void threadSend(Client *client);
    void checksnapshots();
    /*CUT*/
};

The map

typedef std::map<std::string, Client> clientMap;
clientMap clientmap;
  1. Server starts
  2. Boost thread is started(1) that is constantly checking values. The idea is that if certain things happen on the server, all qualifying clients get notified. To make this happen, the message is added to the client class’s deque.
  3. Server is constantly accepting new client connections, which each get their own thread (ClientThread).
  4. Client object is created inside that thread(2)
  5. Client class’ construct starts yet another thread that is constantly sending messages stored in the client-class’ object’s deque (3).

(1) The boost thread that is created in main()

void alwaysWatching()
{
    while(1)
    {
        /*CUT*/
        /* When something that needs to be communcated happens, a message will be formed and stored in the string "thaString" and sent to, in this case, all clients*/  
        for (clientMap::iterator it2 = clientmap.begin(); it2 != clientmap.end(); ++it2)
        {
                it2->second.snapShotsQueue.push_back(thaString); //Add to client's deque
                //Check how many items are in deque
                std::cout << "There are now ";
                it2->second.checksnapshots();                           
                std::cout << "Snapshots waiting according to outside watcher" << std::endl;
        }
        /*CUT*/
    }
}

(2) Creating and adding the client object to the map

DWORD WINAPI ClientThread(LPVOID lpParam)
{
    SOCKET        sock=(SOCKET)lpParam;

    /*CUT*/

    std::string clientID = "";
    std::stringstream ss;
    ss << lpParam; //Socket = clientID
    clientID = ss.str();

    Client client(clientID,sock,false); //Create object for this client

    while(1) //This thread is constantly waiting for messages sent by the client
    {
        /*CUT*/
        //Add clientID to map of clients
        if(clientAdded == false)
        {
            /*CUT*/
            clientmap.insert(std::pair<std::string,Client>(clientID,client));
            clientAdded = true;
            /*CUT*/
        }
        /*CUT*/
    return 0;
}

(3) The thread that sends all messages in the deque to the client

//Struct used to create the thread that will keep on sending messages in deque to the client
struct messageSender
{
    messageSender(Client *client) : client(client) { }

    void operator()()
    {
        client->threadSend(client);
    }
    Client *client;
};

//Client constructor
Client::Client(std::string clientIDs,SOCKET socks,bool quits)
{
    /*CUT*/
    this->group = new boost::thread_group; //Create boost thread group (for later, possibly)
    messageSender startit(this); //Prep new thread for sending snapshot updates
    group->create_thread(startit); //Start new thread for snapshot updates
    /*CUT*/
}

//The actual function that constantly loops through the deque
void Client::threadSend(Client *client)
{
    /*CUT*/
    while(1)
    {
        /*CUT*/
            std::cout << "There are now ";
            client->checksnapshots();
            std::cout << "Snapshots waiting according to class thread queue processor" << std::endl;
        /*CUT*/

        unsigned int i;
        for(i=0; i < client->snapShotsQueue.size(); i++)
        {
            std::string theString;
            theString = client->snapShotsQueue.front();  // this gets the front of the deque
            client->snapShotsQueue.pop_front();             // this removes the front of the deque

            std::cout << "sending: " << theString << std::endl;
            client->sendMessage(theString);
        }
    }
}

As you can see, I added a piece of code that counts the deque in both the thread outside of the class, as well as inside of the class. They both report different counters and the messages from the thread outside of the class aren’t being sent.

enter image description here

So it seems the watcher thread (1) has its own instance of the Client object even though it’s stored inside of the map. Or something in that direction.

I’m probably doing something wrong pointer-wise. Any ideas?

  • 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-27T15:25:10+00:00Added an answer on May 27, 2026 at 3:25 pm

    You’re copying Clients into the map, yes, but then whenever you read them out, you’re implicitly creating new Clients by copying the ones in the map. The new copies will have separate queues of snapshots.

    You probably want to be using either std::map<std::string, Client *> or std::map<std::string *, Client *>, and allocating all of your Clients using new Client(...) (with corresponding deletes). Then, for each client you put in the map, there can be just a single Client instance with multiple copies of pointers to it.

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

Sidebar

Related Questions

Well, the title more or less says it all. I sort of understand what
The title sort of says it all. I have a text file (NOT a
Like the title says I need to write a function that will sort a
hey guys, like the title says, how can i accomplish this sort of a
The title pretty much says it all. I'm using a TClientDataset to store an
Just as the title says. I tried messing around a bit with Collections.sort() on
I am hoping that this is possible and easy! As the title says, I
The title sort of sums it up. I was thinking of using Jabaco seeing
I got this code to sort an unordered list with regards to their title
Title pretty much describes it, I just need to get all the order numbers

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.