Author Topic: Loader - thread safe  (Read 2621 times)

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Loader - thread safe
« on: January 26, 2013, 04:23:30 pm »
Is Loader thread safe? I tried to load objects in many threads for more speed, but something is wrong...

My test case
http://dl.dropbox.com/u/26148874/TestProject.rar

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11769
    • View Profile
    • http://www.jpct.net
Re: Loader - thread safe
« Reply #1 on: January 26, 2013, 08:21:29 pm »
Loader should be thread safe. But your threading code is strange. You aren't doing any multi-threading in it (because you are calling run() instead of start()) and if you were, you aren't waiting for the loaders to finish. Have you tried something like

Code: [Select]
List<MyLoader> loaders=new ArrayList<MyLoader>();
for (int i = 0; i < cpus; i++) {
MyLoader loader = new MyLoader();
loaders.add(loader);
loader.start();
}

for (MyLoader loader:loaders) {
try {
loader.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

instead?

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Loader - thread safe
« Reply #2 on: January 26, 2013, 09:18:45 pm »
Oh sorry, I looked at the code and I found one more bug :-[ It seems compile is done when object is rendered, is possible to do it when is called method Object3D.compile() or maybe Object3D.compile(FrameBuffer)? GPU can be control by one thread, so maybe it not possible...
« Last Edit: January 26, 2013, 09:21:11 pm by Thomas. »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11769
    • View Profile
    • http://www.jpct.net
Re: Loader - thread safe
« Reply #3 on: January 26, 2013, 09:43:17 pm »
No, not possible. It has to happen in the thread that is associated with the gl context.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Loader - thread safe
« Reply #4 on: January 30, 2013, 12:36:58 am »
I got this message when I was loading objects

Code: [Select]
01-30 00:27:54.383: W/System.err(929): java.util.ConcurrentModificationException
01-30 00:27:54.383: W/System.err(929): at java.util.HashMap$HashIterator.nextEntry(HashMap.java:792)
01-30 00:27:54.383: W/System.err(929): at java.util.HashMap$KeyIterator.next(HashMap.java:819)
01-30 00:27:54.383: W/System.err(929): at java.util.HashSet.<init>(HashSet.java:76)
01-30 00:27:54.383: W/System.err(929): at com.threed.jpct.TextureManager.getNames(TextureManager.java:413)
01-30 00:27:54.383: W/System.err(929): at com.threed.jpct.DeSerializer.deserialize(DeSerializer.java:136)
01-30 00:27:54.383: W/System.err(929): at com.threed.jpct.Loader.loadSerializedObjectArray(Loader.java:108)
01-30 00:27:54.383: W/System.err(929): at cz.chladek.mygame.util.ObjectLoader.loadObject(ObjectLoader.java:969)
01-30 00:27:54.383: W/System.err(929): at cz.chladek.mygame.util.ObjectLoader.access$3(ObjectLoader.java:955)
01-30 00:27:54.383: W/System.err(929): at cz.chladek.mygame.util.ObjectLoader$LoaderCore.run(ObjectLoader.java:206)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11769
    • View Profile
    • http://www.jpct.net
Re: Loader - thread safe
« Reply #5 on: January 30, 2013, 08:29:12 am »
I looked at the Loader only when talking about thread safety. And while this one should be safe, the TextureManager might not...i'll have a look but i actually don't want to clutter it with synchronized all over the place....i'll report back this afternoon.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11769
    • View Profile
    • http://www.jpct.net
Re: Loader - thread safe
« Reply #6 on: January 30, 2013, 08:10:16 pm »
I've update the beta jar with a version that adds more synchronization to the TextureManager. I tried to keep it as low a possible, so maybe i've missed something...please give it a try.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Loader - thread safe
« Reply #7 on: January 30, 2013, 08:33:29 pm »
It seems be fine now, thanks ;)