Since I don't have much knowledge of multi-threaded design and English is not my native language, I don't know how to exactly describe my problem. Let's say I have a mini-program in school, which has the responsibility to analyze a runtime feed. The feed is a continuous list of messages with the following format:
{"type": "A", "data": [...]} // message for doing task type A
{"type": "B", "data": [...]} // message for doing task type B
{"type": "C", "data": [...]} // message for doing task type C
...
For each message, I need to compute the corresponding task and then log the result to the <task-type>.txt file. Currently, my program synchronously does these tasks line by line.
However, the problem is:
- there are about 100 unique tasks (no data sharing) for 100 types, I believe it is a waste if not apply parallelism.
- for each type, the corresponding task requires the previous state. For example, given task type A which computes the
XfromdataandX(n-1)from the last message if any, the flow is
message 1: {"type": "A", "data": [...]} --> compute X from data --> log X1 to file
message 2: {"type": "A", "data": [...]} --> compute X from data and X1 --> log X2 to file
message 3: {"type": "A", "data": [...]} --> compute X from data and X2 --> log X3 to file
How can I efficiently apply multi-threaded design to my program? What are the multi-threaded keywords that I can search for this problem (thread pool, work-stealing queue, etc.)?
Edited 06.18.2022: My program should achieve the following requirements:
- read the feed sequentially, parse each message, and assign the correct task for the
typeparsed from the message. - parallelizing the tasks with the awareness that if there is a running task with the same type, this new task needs to wait until the previous task is done.
I am stuck on the solution for step 2. I don't think creating a thread for each type is a good idea because I will make too much thread. So this leads me to a thread pool. But which kind of thread pool can meet the requirement mentioned in step 2?
Is there a generalized term for this problem that I can search for?