Basic C++ Multithreading -


i'm in design phase of development , considering using multi-threading in c++ implement functionality. i'm familiar basics of multi-threading wanted others take on idea. haven't chosen multi-threading library yet (leaning towards boost) question independent of library chosen.

basically have class (let's call commandgenerator) executes in while loop (until terminated) , checks message queue of commands populated piece of software. every time commandgenerator gets message off queue, i'd spawn thread executes in background , works data pulled off queue. meanwhile want commandgenerator continue run , come around while loop again , pull new messages , again spawn more threads. conceptually possible? can keep spawning threads , let them run in background until complete while code continues loop , check queue? commandgenerator not need have control on threads. able execute independently once created , guaranteed terminate may take minute finish executing (they wait amount of time specified in message pulled off queue before executing).

any input appreciated.

what want called "producer-consumer" pattern.

i advise against creating new thread each message received: if did , many messages @ once can clog machine.

instead, have fixed number of consumer threads read message queue, , let them handle 1 message @ time. if many messages come @ single time stored in queue, waiting processed.

since have delay between fetching message , processing it, again solution imho not spawn 1 thread per message increase number of consumer threads. way can keep resource usage under control. how many threads need totally dependent on application, have find out yourself.

as implementation, if use c++11 need std::thread / std::mutex , std::condition_variable. if use c++03 boost has equivalent classes.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -