Thread: Std Threading In A Windowed Application

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    139

    Std Threading In A Windowed Application

    Hello,

    I am trying to write a gui application that is threaded and I am trying to write a tcp listner. I want the tcp listner to run on a different thread but I am having some trouble.

    For testing purposes I wrote a simple example that uses std::thread.

    This code shows a blank message box (msg is never shown)
    Code:
    void task1(std::string msg)
    {
        wxMessageBox(msg);
    }
    
    
    void TestApplicationDialog::OnButton2Click(wxCommandEvent& event)
    {
        std::thread t1(task1, "Hello");
        t1.join();
    }

    So i tried to put it in the same namespace but it errors

    Code:
    void TestApplicationDialog::task1(std::string msg)
    {
        wxMessageBox(msg);
    }
    
    
    void TestApplicationDialog::OnButton2Click(wxCommandEvent& event)
    {
        std::thread t1(task1, "Hello");
        t1.join();
    }
    Code:
    error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, const char [6])’|
    Does anyone know how I can get information from one thread back to the main gui?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The error you receive is because you cannot simply pass a non-static class member function pointer around. You need an object to go with it.

    This is one possible solution:
    Code:
    void TestApplicationDialog::task1(const std::string& msg)
    {
        wxMessageBox(msg);
    }
     
     
    void TestApplicationDialog::OnButton2Click(wxCommandEvent& event)
    {
        std::thread t1([this]{ task1("Hello"); });
        t1.join();
    }
    Note the change to the string parameter of task1(). Strings and all other class types should be passed by reference, and const reference, whenever possible.

    To get information from one thread back to the main gui, you could use a std::future.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    can you tell me why this does not count like I was expecting it to?





    Code:
    using boost::asio::ip::tcp;
    
    const int max_length = 1024;
    
    typedef boost::shared_ptr<tcp::socket> socket_ptr;
    
    boost::thread *thListener;
    
    void session(socket_ptr sock)
    {
      try
      {
        for (;;)
        {
          char data[max_length];
    
          boost::system::error_code error;
          size_t length = sock->read_some(boost::asio::buffer(data), error);
          if (error == boost::asio::error::eof)
            break; // Connection closed cleanly by peer.
          else if (error)
            throw boost::system::system_error(error); // Some other error.
    
          boost::asio::write(*sock, boost::asio::buffer(data, length));
        }
      }
      catch (std::exception& e)
      {
        std::cerr << "Exception in thread: " << e.what() << "\n";
      }
    }
    
    void server(boost::asio::io_service& io_service, unsigned short port)
    {
      tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
      for (;;)
      {
        socket_ptr sock(new tcp::socket(io_service));
        a.accept(*sock);
        boost::thread t(boost::bind(session, sock));
      }
    }

    Code:
    void startListener()
    {
        cout << "thread started"  << endl;
        boost::asio::io_service io_service;
        using namespace std; // For atoi.
        server(io_service, 1501);
        return;
    }
    
    
    int main()
    {
       thListener = new boost::thread{ &startListener };
    
    
      for (int i = 0; i < 50000; ++i)
      {
        sleep(1000);
        std::cout << i << '\n';
      }
    }

    I was expecting that code to start the listener (on another thread) and then start counting but it never counts.
    Last edited by EverydayDiesel; 01-24-2017 at 05:35 PM.

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    the above can be fixed by changing sleep(1000) to sleep(1)

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    In the future, you should start a new thread for a new problem.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. force windowed mode
    By X PaYnE X in forum Game Programming
    Replies: 2
    Last Post: 05-30-2005, 09:48 PM
  2. Windowed Applications
    By Justin C in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2005, 01:01 AM
  3. Adding a skin to a windowed application
    By BruceLeroy in forum Windows Programming
    Replies: 1
    Last Post: 08-24-2004, 03:51 PM
  4. windowed in allegro.
    By Guido in forum Game Programming
    Replies: 0
    Last Post: 04-21-2004, 02:10 PM
  5. D3D Windowed Application
    By Magos in forum Game Programming
    Replies: 6
    Last Post: 03-14-2004, 02:26 PM

Tags for this Thread