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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:05:22+00:00 2026-06-02T04:05:22+00:00

I am reading a message from a socket with C++ code and am trying

  • 0

I am reading a message from a socket with C++ code and am trying to plot it interactively with matplotlib, but it seems Python code will block the main thread, no matter I use show() or ion() and draw(). ion() and draw() won’t block in Python.

Any idea how to plot interactively with matplotlib in C++ code?

An example would be really good.

Thanks a lot.

  • 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-06-02T04:05:24+00:00Added an answer on June 2, 2026 at 4:05 am

    You may also try creating a new thread that does the call to the
    blocking function, so that it does not block IO in your main program
    loop. Use an array of thread objects and loop through to find an unused
    one, create a thread to do the blocking calls, and have another thread
    that joins them when they are completed.

    This code is a quick slap-together I did to demonstrate what I mean about
    using threads to get pseudo asynchronous behavior for blocking functions…
    I have not compiled it or combed over it very well, it is simply to show
    you how to accomplish this.

    #include <pthread.h>
    #include <sys/types.h>
    #include <string>
    #include <memory.h>
    #include <malloc.h>
    #define MAX_THREADS 256 // Make this as low as possible!
    using namespace std;
    pthread_t PTHREAD_NULL;
    typedef string someTypeOrStruct;
    class MyClass
    {
        typedef struct
        {
            int id;
            MyClass *obj;
            someTypeOrStruct input;
        } thread_data;
    
        void draw();    //Undefined in this example
        bool getInput(someTypeOrStruct *);  //Undefined in this example
        int AsyncDraw(MyClass * obj, someTypeOrStruct &input);
        static void * Joiner(MyClass * obj);
        static void * DoDraw(thread_data *arg);
        pthread_t thread[MAX_THREADS], JoinThread;
        bool threadRunning[MAX_THREADS], StopJoinThread;
    
        bool exitRequested;
    public:
        void Main();
    };
    
    bool MyClass::getInput(someTypeOrStruct *input)
    {
    }
    
    void MyClass::Main()
    {
        exitRequested = false;
        pthread_create( &JoinThread, NULL, (void *(*)(void *))MyClass::Joiner, this);
    
        while(!exitRequested)
        {
            someTypeOrStruct tmpinput;
            if(getInput(&tmpinput))
                AsyncDraw(this, tmpinput);
        }
    
        if(JoinThread != PTHREAD_NULL)
        {
            StopJoinThread = true;
            pthread_join(JoinThread, NULL);
        }
    }
    
    void *MyClass::DoDraw(thread_data *arg)
    {
        if(arg == NULL) return NULL;
        thread_data *data = (thread_data *) arg;
        data->obj->threadRunning[data->id] = true;
        // -> Do your draw here <- //
        free(arg);
        data->obj->threadRunning[data->id] = false; // Let the joinThread know we are done with this handle...
    }
    
    int MyClass::AsyncDraw(MyClass *obj, someTypeOrStruct &input)
    {
        int timeout = 10; // Adjust higher to make it try harder...
        while(timeout)
        {
            for(int i = 0; i < MAX_THREADS; i++)
            {
                if(thread[i] == PTHREAD_NULL)
                {
                    thread_data *data = (thread_data *)malloc(sizeof(thread_data));
                    if(data)
                    {
                        data->id = i;
                        data->obj = this;
                        data->input = input;
    
                        pthread_create( &(thread[i]), NULL,(void* (*)(void*))MyClass::DoDraw, (void *)&data);
                        return 1;
                    }
                    return 0;
                }
            }
            timeout--;
        }
    }
    
    void *MyClass::Joiner(MyClass * obj)
    {
        obj->StopJoinThread = false;
        while(!obj->StopJoinThread)
        {
            for(int i = 0; i < MAX_THREADS; i++)
                if(!obj->threadRunning[i] && obj->thread[i] != PTHREAD_NULL)
                {
                    pthread_join(obj->thread[i], NULL);
                    obj->thread[i] = PTHREAD_NULL;
                }
        }
    }
    
    int main(int argc, char **argv)
    {
        MyClass base;
        base.Main();
        return 0;
    }
    

    This way you can continue accepting input while the draw is occurring.

    ~~Fixed so the above code actually compiles, make sure to add -lpthread

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

Sidebar

Related Questions

I am trying to read from a tcp stream a message sent from client.
Hi I am trying to send a simple HTTP message from Flex to C#
I have some code that I'm using to get data from a network socket.
I have the following code (I'm working from code at http://www.linuxhowtos.org/C_C++/socket.htm ) which I'm
The following code sends messages from child processes to their parents using socket pairs.
I am building a WinForms application reading from a socket data and charting lines.
I've been reading up on message queueing lately, and I'd like to implement a
I'm using websphere and Java EE and get this error message: Exception reading propertiesfile
reading the documentation for java org.w3c.dom.ls it seems as a Element only can be
Reading some posts from Jimmy Boggard and wondering - how exactly is it possible

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.