niktehpui
niktehpui

Reputation: 556

Concurrent Event Processing Problems

I just started learning multi-threading in general and I know that the example application - I am working on - might even decrease in performance when using threads. The used Compiler is GCC4.7 with following flags: -std=c++0x -g -O3 -Wall

Therefore I am working with C++11 and use the std::thread implementation. In general I try to develop cross-platform, so I develop on Windows on University Computers using GCC4.7 as well and Ubuntu/Mac OSX on my home machine. To be able to handle Window creation cross-platform I use the SFML-library (2.0).

Here the simplified CApp Class and related functions:

class CApp
{
public:
    //! Constructor.
    CApp(void);
    //! Deconstructor.
    ~CApp(void);

    /* Public Functions: */
    //! Initializer function, needs to be called before Run-Function to allocate everything and set everything up.
    bool Initialize(unsigned int width, unsigned int height, char* title, bool vsync, CState* startState);
    void Run(void); //!< Starts the game-loop
private:
    void ProcessEvent(sf::Event event);

    sf::RenderWindow*   m_pWindow;      //!< window instance
    std::vector<std::thread> m_threads;
};

In general my application is a small OpenGL Demo Scene, that should be heavily multi-threaded to be able to benchmark and compare where multi-threading is suited and where it is not.

The implementations of the concerned functions:

void CApp::Run(void)
{
    while (m_pWindow->IsOpen())
    {
        sf::Event event;
        while (m_pWindow->PollEvent(event))
        {
            m_threads.push_back(std::thread(&CApp::ProcessEvent, this, event)); 
        } 
        // ...
        m_threads.clear();
    }
}

void CApp::ProcessEvent(sf::Event event)
{
    if (event.Type == sf::Event::Closed)
        m_pWindow->Close();
    if (event.Type == sf::Event::KeyPressed) 
    {
        if (event.Key.Code == sf::Keyboard::Escape)
            m_pWindow->Close();
    }
}

The runtime error I get is simply an Abort trap: 6 on the first frame: _"terminate called without an active exception - Program received signal SIGABRT, Aborted. 0x00007fff8fdf4ce2 in __pthread_kill ()"_

If I outcomment the thread creation and just call ProcessEvent in the current thread, no problem occurs, so the problem must be thread related.

SFML isn't all in all thread-safe, so I assume the problem is the m_pWindow-Pointer or the event as function argument itself, but how do I solve this problem?

Upvotes: 1

Views: 304

Answers (1)

Stephan Dollberg
Stephan Dollberg

Reputation: 34528

Before clearing the vector and such deleting all the threads, you should definitely wait for all threads to finish:

void CApp::Run(void)
{
    while (m_pWindow->IsOpen())
    {
        sf::Event event;
        while (m_pWindow->PollEvent(event))
        {
            m_threads.push_back(std::thread(&CApp::ProcessEvent, this, event)); 
        } 

    for(auto& i : m_threads)
      i.join();

    m_threads.clear();
   }
}

In addition, when you are using GCC I would recommend to compile with the -pthread option.

Upvotes: 3

Related Questions