Can threads access user defined types?
BlitzMax Forums/BlitzMax Beginners Area/Can threads access user defined types?
| ||
I'm attempting to get a thread to load a bunch of large images in a loop. But I keep getting unhandled exception errors. Any tips? Example: Edit: I realize that the type is outside of the threaded code, but what is a ggod way to handle the loading of many images in a separate thread, then allowing those images to be accessable by the main thread? |
| ||
And totalimages is more than 0 when? |
| ||
Thanks for checking in. I actually answered my own question: YES, different threads can access user defined types created by the main thread-- if you set up your type correct (i didn't). I had read elsewhere that calling type methods may cause GC problems? Don't know if this is true. My program still gets "exception access violation" errors, but the debugger isn't picking up from where. Updated code snippet: |
| ||
you must protect any object from concurrent access through Mutex or Semaphore. Normally one would likely implement a monitor to achieve concurrent access to objects in a maintanable way instead of generating 2 million mutexes, but if you want the mutex way, you could use TMap and store the mutex for the object by using the object reference itself as key |
| ||
THanks for the extra info. It seems the GC is causing trouble, if I add in: GCCollect() ...it kills my program. I thought that maybe if I had the type created by the main thread that it would be protected from concurrent access, and protected from the GC from clearing anything out? |
| ||
there is nothing protected unless you protect it. if you do a concurrent access the application commonly dies (unless you only did read access), not because BM is mean but because it will cause a memory violation on the lowest level and get killed. the GC will not clear anything to which a valid reference is present at the time. |
| ||
Thanks for your help! I'm still confused on what's protected and what's not. Here's the high-level of my app: Overall, the program loads and views thumbnails of images. There's a lot of images and they're large. So I have my main thread and it sets up my storedimage type. The main thread then does its thing, and eventually creates a new thread, calling a function called loadthumbs. In loadthumbs, i have a loop for the list of images in a directory. it loads an image (freeimage), resizes it, and then calls a method in storedimagetype to place it the allocated memory. This method is never called by the main thread, and I've mutexed loadthumbs. I'm not creating a separate thread for each imageloader, but rather just setting a loop so I can go and do other things. Now, the main thread could call storedimagetype to see if the image exists and then assumes the image is loaded. Is this considered protected? Can a Mutexed function thread call another function that's being used by perhaps another thread and cause conflict? Even though the function is only using local variables? |
| ||
Well, small update: by using GCCollect() in strategic points, I've been slowly finding where a pixmap was being passed, but when later released, it lost the pixmap. I'm not sure if this is a threading problem or Freeimage problem, but at least I now have a method of helping debug the darn thing. I'm still getting errorless crashes, though. I'm on BM 1.37 and Vista64. |
| ||
Oh, an error popped up: GC ERROR: clrMemBit error: mempage does not exist, object=$0053A470 clrMemBit error: mempage does not exist |
| ||
Ok, the problem is within FreeImage (w/threading), so thank anyone who helped. |