Suggestion on Multi-Threading practices

Recently the multithreading drives me crazy......

I have 3 main thread in the app.

1: message thread

2: network request pool

3: usb message thread

Thread 1 (message thread) calls a method to create a threadpoolJob to fetch content from network and use callback (normal pointer callback) to notify back to thread 1 again (not sure whether it's thread 1....   Question1),  and then in the callback I control the UsbManager to do something (start listener etc)

and when app shutting down, it always tells me "Free Heap block xxx modified at xxx after it was freed"

so I set a waitableEvent for the manager class which own the thread 2 (network Jobs thread) deconstructor and let the class own UsbThread to set signal for the event after the usbthread stopped, so always keep the UsbManager(UsbThread) destroy first, and the error message no longer shows up again. (Question2)

 

so my question:

Q1: here the current thread is 2 or 1? I don't quit understand about resource destory (life cycle) on a thread.

Q2: why I need to control the destory sequence? again I think it's something about resource life on a thread...

Q3: I need some basic praticle on multi-threading, any suggestion? especially about resource control. (also thread synchronization, but lower priority)

 

I don't know if you guys could understand what I'm talking about.. sorry for the bad english.

 

Q1/2.  I don't understand exactly what you are asking.  But it should be possible to control the exit order of things using ThreadPooJob::shouldExit(), ThreadPool::removeAllJobs(). 

If you need to know more about what's happening you could

  • implement destructors for your objects
  • put breakpoints in them (or put jassertfalse;)
  • run in debug mode and let the debugger explain what's going on - it should tell you what thread things are happening on.

Q3.

Herb is good to listen to about threads: 

http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-1-of-2

I found the talk a lot easier to follow than this book:

http://www.amazon.com/C-Concurrency-Action-Practical-Multithreading/dp/1933988770

The book has some awful 10 clause sentences.  Often right in the middle of an important topic.  I'd start with Herb's CPPCON presentation.

 

Thank you Bazrush, I'll take a look and do some basic praticles.