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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:00:51+00:00 2026-05-30T23:00:51+00:00

Im working on a client (that use a GUI and a socket). The GUI

  • 0

Im working on a client (that use a GUI and a socket). The GUI (running on the main thread) create on the fly commands of my protocol, every time I create a new command I append it in a dynamic stack.

In another thread (using pthread), I check if there is a command in the stack then send it progressively using a socket (which is non-block btw). When the command on the top of the stack is done sending I pop the stack (which means that the command is removed and each commands below the first element are pushed up).

Now as you might guess I have huge problems to synchronize that… I try to use some flags, mutex but I always end up with some access violation on the memory.

Basically what Im doing is this:

In the main thread when (ie) the user press a button:

char cmd[ 50 ] = "My new protocol command";

AppendCommandToSendStack( cmd );

In another thread:

if( SendStackCount )
{
     SendCommand( 0 ); // 0 = The top of the stack.

     if( IsCommandDoneSending( 0 ) )
     {
          RemoveCommandFromStack( 0 );
     }
}

Can anybody help me or give me some pointers on how to make this mechanism work. Or another approach that would result in the same or similar workflow as Im trying to implement.

Tks in advance!

[ Update ]

I start reading on semaphore and it seems thats exactly what I need… however it doesn’t seems to work… this is what Im doing in pseudo code:

sem_t mutex;

int stack_count;

command *stack;

main()
{
    // Init & Connect client.

    // Start Thread

    sem_init( &lock_stack, 0, 1 );

    while( 1 )
    {
        sem_wait( &lock_stack );

        ++stack_count;

        stack = ( command * ) realloc( stack, stack_count * sizeof( command ) );

        strcpy( stack[ stack_count - 1 ].cmd, "new command" );

        sem_post( &lock_stack );
    }
}


thread()
{
    while( 1 )
    {
        sem_wait( &lock_stack );

        if( stack_count )
        {
            // Send stack[ 0 ].cmd via socket then when done pop the stack like this:

            --stack_count;

            if( 0 < stack_count )
            {
                memcpy( &stack[ 0 ],
                    &stack[ 1 ],
                    ( stack_count - 1 ) * sizeof( command ) );
            }        
        }

        sem_post( &lock_stack );
    }
}

Since Im basically new to this am I missing something obvious, because I still have memory access violation that “seems” to happen randomly on the stack array.

  • 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-30T23:00:52+00:00Added an answer on May 30, 2026 at 11:00 pm

    You have a classic example of producer-consumer problem. You want to produce commands, while other threads consume them. You can have a look at http://en.wikipedia.org/wiki/Producer-consumer_problem which explains how to implement a basic producer-consumer problem.

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

Sidebar

Related Questions

I'm looking for a TFS 2010 GUI client that I can use outside of
I'm working with an enterprise client right now that makes use of the Catalog
I'm working with a client that needs to generate millions of the alphanumeric codes
We are working on a website for a client that (for once) is expected
I am working on a client-server application that uses boost::serialization library for it's serialization
Folks, I'm working on a little piece of rich client software that I'd like
I have a SOAP client in Ruby that I'm trying to get working with
Environment I'm working on a C++ application that uses SQL Native Client 9.0 to
We're working on a rich client (written in Flex) that is connected to a
I am working with an application that is used by multiple clients. Each client

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.