2015-10-04 74 views
1

所以使用即時通訊和GLFW以下方法作品時叫我的主要方法線程化該方法會產生段錯誤,爲什麼?

void Display::run() { 
    while (!glfwWindowShouldClose(window)) 
    { 
     /* Render here */ 



     /* Swap Buffers And Poll */ 
     glfwSwapBuffers(window); 
     glfwPollEvents(); 
    } 
} 

但是當我嘗試在一個單獨的線程中運行它,我得到一個segfault

std::thread t1(&Display::run, this); 

什麼想法?詢問是否要更多的代碼

編輯的: 的main.cpp

#include "src/support/Display.h" 

int main() { 
    Display* d; 
    d->start(); 
    return 0; 
} 

Display.h

#include <GLFW/glfw3.h> 
#include <exception> 
#include <thread> 


class Display { 

private: 
    GLFWwindow* window; 
    std::thread* displayThread; 

    void run(); 

public: 
    Display(); 
    void start(); 
    void close(); 
}; 

/* Exceptions */ 
struct GLFWNotInitilizedException : public std::exception 
{ 
    const char * what() const throw() 
    { 
     return "ERR: Could Not Initialize GLFW"; 
    } 
}; 

struct WindowNotCreatedException : public std::exception 
{ 
    const char * what() const throw() 
    { 
     return "ERR: Could Not Create Window"; 
    } 
}; 

Display.cpp

#include "Display.h" 

Display::Display() { 
    //init glfw 
    if (!glfwInit()) 
     throw GLFWNotInitilizedException(); 

    //create window and its context 
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); 
    if (!window) 
    { 
     glfwTerminate(); 
     throw WindowNotCreatedException(); 
    } 

    //make windows context current 
    glfwMakeContextCurrent(window); 

    //run(); //this works instead of calling start() which puts run() into a thread 

} 

/* begins the rendering of the display window contents in seperate thread */ 
void Display::start() { 
    std::thread t1(&Display::run, this); 
    displayThread = &t1; 
} 

/* renders contents of display window */ 
void Display::run() { 
    while (!glfwWindowShouldClose(window)) //seg fault is output here 
    { 
     /* Render here */ 



     /* Swap Buffers And Poll */ 
     glfwSwapBuffers(window); 
     glfwPollEvents(); 
    } 
} 


/* to be used when closing display window */ 
void Display::close() { 
    glfwSetWindowShouldClose(window, true); 
    displayThread->join(); 
    glfwDestroyWindow(window); 
} 
+2

我們不應該要求查看更多的代碼,請閱讀http://stackoverflow.com/help/mcve和http://kera.name/articles/2013/10/nobody-writes-testcases-任何更多/和http://blog.jerryorr.com/2014/04/solve-your-problem-by-almost-asking.html –

+0

@JonathanWakely希望這會讓它更清晰,對不起,如果我沒有問正確的方式,因爲我從來不曾訴諸詢問本網站 –

+0

d-> start();使用垃圾指針,你沒有指向任何東西 –

回答

1
Display* d; 

你沒有創建一個ob ject在這裏,只是一個未初始化的指針。

d->start(); 

這會調用不存在對象上的成員。當它試圖訪問run()函數中的任何成員時,它只是訪問垃圾,因爲沒有對象。

你可能想創建一個對象,像這樣:

Display d; 
d.start(); 

而且你的啓動功能將終止該計劃,因爲你被銷燬之前不加入線程。在嘗試使用像這樣的線程和指針之前,您應該學習C++對象生命週期的基礎知識。

停止使用指針,直到你理解基礎。 displayThread應該是一個實際的std::thread,不僅僅是指向std::thread超出範圍的指針。

然後,你可以這樣做:

void Display::start() { 
    displayThread = std::thread(&Display::run, this); 
} 

,請務必讓displayThread.join()它被摧毀之前,e.g在Display析構函數。

2
//make windows context current 
glfwMakeContextCurrent(window); 

這使得活躍在CURRENT線程。在你想渲染的線程上運行它,它應該工作。目前它在構造函數中。

+0

這解決了我的問題。謝謝! –

相關問題