如何在多线程中管理线程 [英] How to manage threads in Multithreading

查看:171
本文介绍了如何在多线程中管理线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我是一名矿物工程师。我正在编写用于模拟某种系统的代码。整个程序包含一个无限循环,当我退出程序时它将被停止。在我的代码中,我现在正在使用3个线程。第一个线程只显示模拟结果,第二个线程将我的模拟结果保存到文本文件中,最后一个线程正在计算系统进程,我将其命名为Main Simulation Thread(MST)。

每次迭代中的MST执行必须在队列中执行的三个任务。

第一个任务是初始化系统元素,第二个任务是计算元素交互,第三个任务是完成迭代结果。 br />
现在我想通过雇用8个线程来完成MST的第二个动作(计算元素交互)来使代码更快。

很明显,我必须考虑任务之间的队列,因此我必须管理新线程,以便在MST完成初始化任务时启动,并且当新线程正在执行时,必须停止MST作业,当新线程作业完成时,MST应该完成第三项任务(最终确定迭代结果)。

我不知道如何管理这些线程。

请帮帮我。

Hi everybody
I am an minerals engineer. I am programming a code for simulating a kind of system. the whole program contains a infinite loop and it will be stopped when I exit my program. In my codes, I am using 3 threads right now. The fist thread just displaying the simulation results, the second thread is saving my simulation results to a text file and the final thread is calculating the system process that I named it Main Simulation Thread (MST).
The MST in each iteration do three tasks that must be performed in queue.
the first task is initialize the system elements, the second task is calculating the elements interaction and the third task is finalizing the iteration results.
Now I want to make my code faster by hiring 8 threads to do the second action of MST (calculating the elements interaction).
It is clear that I have to consider the queue between tasks, so I have to manage the new threads to start when initialize task was done by MST and MST must be stopped when the new threads is doing their job and when the new threads job was finished the MST should do the third task (finalizing the iteration results).
I don't know how to manage these threads.
Please help me.

推荐答案

你要做什么叫线程同步互斥取决于具体细节关于调度。



这是一个线程必须等到另一个线程到达给定点才恢复之前。



使用交错锁定管理8个线程可能会很复杂。
What you want to do is called thread synchronisation or mutual exclusion depending on details about the scheduling.

This is when a thread have to wait until another thread reach a given point before resuming.

It may get complicated to manage 8 threads with interleaved locking.


这篇关于如何在多线程中管理线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆