OpenGL 0x00000000 on glDrawArrays with threads

I have an std::map that stores pointers to classes. I have the main program and a separate thread. I need to load files into the map from the thread, but I need to prevent the main program from accessing that particular key while it is being loaded up. How should I go about doing this?

EDIT:

The problem had to do with OpenGL, and it was that vertex arrays that are generated in one thread cannot be shared with another thread. You will have to re-generate it from the thread you are rendering from afterward.
Last edited on
Hi,

I don't really know anything much about this, but is that what a mutex is for? I really only giving a starting point to go looking further, hope it helps a little.

http://en.cppreference.com/w/cpp/concept/Mutex
http://en.cppreference.com/w/cpp/thread/mutex

Good Luck !!

http://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=mutex
Last edited on
I've tried using mutexes... I must be doing something wrong because they don't seem to be helping. I've tried locking the mutex before I start adding the new key and value and unlocking when I'm done. Is that the correct use?
Is that the correct use?
Yes, better use lock_guard:

http://www.cplusplus.com/reference/mutex/lock_guard/

Remember that you need to lock the mutex always when you access the map.

What are the actual problems?
OP: quite honestly your information is threadbare to give you any sensible suggestion - ' ... map that stores pointers to classes.' OK does that mean there's some polymorphism here? What does 'load files into the map' mean? Load into std::map::value I suppose, so polymorphic files or what? Re 'prevent main program from accessing ... key ... ' what is the key type etc? Too many missing pieces of the jigsaw
Hmm, I haven't been able to reproduce the problem with new code, and what I have is way too large for anyone to try to read. In response to gunnerfunner, I left out some details because they aren't necessary (or so I thought, maybe they will turn out to be.)

The exact error that I'm getting is "Exception thrown at 0xSOMETHIN in PROGRAM: 0xC0000005: Access violation reading location 0x00000000." which, if I am correct, means that something tried to dereference a nullptr.

If a thread is loading a large class to a map that might take about a second, then will another thread trying to access that class get a class that is not done being constructed? Because that's the only thing I can think of that could be causing the problem.

EDIT: if this is the case, then I would need to prevent the main thread from trying to read the key and value that the loader thread is currently working on.
Last edited on
if this is the case, then I would need to prevent the main thread from trying to read the key and value that the loader thread is currently working on.
Yes, a shared object needs to be always protected. It doesn't matter whether in the main thread or in any other thread. Check carefully if more obects are shared. Usually each object has its own mutex.
OP wrote:
If a thread is loading a large class to a map that might take about a second, then will another thread trying to access that class get a class that is not done being constructed?


This makes me suspect that you have an issue with the design of your program flow. Multi-threading is neat, but people often over estimate the benefits gained from it. A lot of the time, Multi-threading doesn't add anything except complexity to a program.
Well the problem had to do with OpenGL. I did not provide enough information here for it to be solved correctly. The problem was that a Vertex Array was being deleted as it was passed to the other thread. When I tried to use glDrawArrays, it gave me an error because the vertex array was a nullptr, but it was hard to find because it didn't tell me where. I will edit the title and question to provide more information for future reference.
Topic archived. No new replies allowed.