2009-06-15 44 views
0

我正在研究广泛使用多线程的模拟。事情是,直到现在,我从来没有使用任何互斥对象来保护我的数据。其结果是,我得到一堆段错误的..C++/GLFW - 使用Mutex对象的正确方法?

我试图锁定/使用互斥解锁时:读/写,但引起我的另一个段错误:

#0 77D27DD2 ntdll!RtlEnumerateGenericTableLikeADirectory() (C:\Windows\system32\ntdll.dll:??) 
#1 00000000 ??() (??:??) 

中当然,我创造,我应用锁定/解锁的事情基本情况的测试项目和它的工作,这里是一个基本的例子,说明如何使用处理互斥对象GLFW:

#include <GL/glfw.h> 
#include <iostream> 
#include <vector> 

using namespace std; 

vector<int> table; 
GLFWmutex th_mutex; 
void GLFWCALL Thread_1(void* arg) { 


    glfwLockMutex(th_mutex); 
    table.pop_back(); 
    glfwUnlockMutex(th_mutex); 
} 

void GLFWCALL Thread_2(void* arg) { 

    glfwLockMutex(th_mutex); 
    table.erase(table.begin()); 
    glfwUnlockMutex(th_mutex); 

} 

int main() 
{ 

    bool running = true; 
    GLFWthread th_1, th_2; 

    glfwInit(); 

    if(!glfwOpenWindow(512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) 
    { 
     glfwTerminate(); 
     return 0; 
    } 

    glfwSetWindowTitle("GLFW Application"); 

    for(int i = 0;i < 10; i++) { 
     table.push_back(i); 
    } 


    th_mutex = glfwCreateMutex(); 
    th_1 = glfwCreateThread(Thread_1, NULL); 
    th_2 = glfwCreateThread(Thread_2, NULL); 


    while(running) 
    { 

     // exit if ESC was pressed or window was closed 
     running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED); 
    } 

    glfwTerminate(); 

    return 0; 
} 

的项目,我工作更大,我有5个线程运行,并有很多向量,地图,q ueues在同一时间被访问。某处在代码中,我试图做这样的事情:

void GLFWCALL CreateVehicleThread(void* arg) { 

    int index = (*static_cast<PulseStateByEntrance*>(arg)).index; 
    double t_initial = (*static_cast<PulseStateByEntrance*>(arg)).initial_time; 
    double t_final = (*static_cast<PulseStateByEntrance*>(arg)).final_time; 
    int pulse = (*static_cast<PulseStateByEntrance*>(arg)).pulse; 
    int nb_entrance = (*static_cast<PulseStateByEntrance*>(arg)).nb_entrance; 
    int min_time_creation = static_cast<int>(ceil(3600/pulse)); 


    while((glfwGetTime() - (*static_cast<PulseStateByEntrance*>(arg)).initial_time) 
    < ((*static_cast<PulseStateByEntrance*>(arg)).final_time - (*static_cast<PulseStateByEntrance*>(arg)).initial_time)) { 


      double t_elapsed = glfwGetTime() - t_initial; 


      if(t_elapsed > min_time_creation) { 

       **int nb_vehicle_per_cycle = static_cast<int>((t_elapsed * pulse)/3600); 
       glfwLockMutex(th_mutex); 
       VehicleManager::CreateVehicles(nb_vehicle_per_cycle, nb_entrance); 
       glfwUnlockMutex(th_mutex);** 
       t_initial = glfwGetTime(); 

      } 

    } 


} 

之所以,我把我的VehicleManager:CreateVehicles()锁定/解锁之间的方法是,因为这种方法有这一行:

VehicleManager::vehicles_.push_back(vehicle); 

所以我想保护矢量:vehicles_。但是,结果我得到了上面的段错误。即使有:

glfwLockMutex(th_mutex); 
VehicleManager::vechicles_.push_back(vehicle); 
glfwUnlockMutex(th_mutex); 

我得到了相同的段错误。

我希望我已经让自己清楚,足以让你理解我的问题的本质。我想,并非所有人都与GLFW合作,这就是为什么我给了你第一个基本的例子,所以你可以理解互斥体如何与这个库一起工作。

谢谢!

回答

1

你可以换你的容器,这将使你的代码更容易理解:

template<typename T> 
class MultithreadedVector 
{ 
public: 
    void pushback(T data) 
    { 
     glfwLockMutex(m_mutex); 
     m_container.push_back(data); 
     glfwUnlockMutex(m_mutex); 
    } 
//then similar for erase etc 
private: 
    std::vector<T> m_container; 
    GLFWmutex m_mutex; 
}; 
1

没有足够的信息。但作为猜测,它看起来像互斥体没有正确创建(看起来像一个NULL引用)。找到调用glfwCreateMutex()的点并确保返回有效值。

不相关的问题,让一个侧面说明:

不这样做:

glfwLockMutex(th_mutex); 
<ACTION> 
glfwUnlockMutex(th_mutex); 

这是C风格,因此也不例外安全。
您应该设置一个类,以便在构造函数中调用该锁,并在析构函数中调用解锁。然后有这个类的锁对象。因此,锁定和解锁通常作为对象创建结构的一部分发生,因此在出现异常时锁定将被正确释放。

princeiple是RAII,他们在这里的主题很多文章。

例子:

class MutexLocker 
{ 
    public: 
    MutexLocker(GLFWmutex& mutex) 
     :m_mutex(mutex) 
    { 
     glfwLockMutex(m_mutex); 
    } 
    ~MutexLocker() 
    { 
     glfwUnlockMutex(m_mutex); 
    } 
    private: 
     GLFWmutex& m_mutex; 
}; 

void myAction() 
{ 
    MutexLocker lock(th_mutex); 

    // Do stuff here: 
} 
+0

你能帮我一个片段还是什么?谢谢:) – 2009-06-15 19:18:21

+0

谢谢,我会试一试! – 2009-06-15 19:51:27

相关问题