Well, using UnregisterClassA is dangerous because it requires that the hotplug 
thread always exits correctly and that you are not starting a new hotplug 
thread before the old one has ended. And this is where the problems are:

In the error path of windows_init you're just terminated the hotplug thread 
using TerminateThread. This can result in killing the hotplug thread after the 
window class was registered but before the thread had a chance to unregister it 
again. The next call to windows_init will try to start the hotplug thread 
again, this will fail because the window class is still registered from the 
last attempt. You cannot just terminate a thread right away, this will just 
create trouble.

Also you're not doing any handshaking with the hotplug thread in windows_init. 
You just hope that it'll work. If there is an error in starting the hotplug 
thread then windows_init will just succeed but hotplug doesn't work, because 
the hotplug thread exited early. The clock_gettime thread uses a semaphore to 
handshake between the thread and windows_init. The hotplug thread should do the 
same.

Looking at windows_exit I see that you only ask the hotplug thread to exit if 
it managed to create the message window yet. But if the hotplug thread didn't 
manage to create the message window yet (due to the missing handshake in 
windows_init and unfortunate thread scheduling) you're not going to end it. The 
next call to windows_init will create a second hotplug thread which will then 
exit early due to the inability to register the window class a second time, as 
the first hotplug thread is still running.

So I suggest that you add handshaking to the hotplug thread and make 
windows_init wait until the hotplug thread has created the message window so 
that you can always ask the hotplug thread to exit via WM_QUIT. Basically use 
the same pattern as the clock_gettime thread.

---
Reply to this email directly or view it on GitHub:
https://github.com/libusbx/libusbx/issues/9#issuecomment-26168408
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to