我正在使用ThreadStatic变量来存储一些数据,但是我担心在完成它并释放回ThreadPool后,我在线程上存储的数据仍然存在.在完成线程之前,我是否需要担心清除ThreadStatic变量?或者ThreadPool会在为下一个QueueUserWorkItem"传递出去"之前为我做这个吗?这对我来说尤其重要,因为我需要确保我的应用程序中的其他线程有一个干净的平板可以使用ThreadStatic变量.谢谢!
我正在编写一个简单的应用程序(对于我的妻子而言:-P),它可以对潜在的大批图像进行一些图像处理(调整大小,时间戳等).所以我正在编写一个可以同步和异步执行此操作的库.我决定使用基于事件的异步模式.使用此模式时,您需要在工作完成后引发事件.这是我知道什么时候完成的问题.所以基本上,在我的DownsizeAsync方法(缩小图像的异步方法)中,我正在做这样的事情:
public void DownsizeAsync(string[] files, string destination)
{
foreach (var name in files)
{
string temp = name; //countering the closure issue
ThreadPool.QueueUserWorkItem(f =>
{
string newFileName = this.DownsizeImage(temp, destination);
this.OnImageResized(newFileName);
});
}
}
Run Code Online (Sandbox Code Playgroud)
现在棘手的部分是知道什么时候它们都完整了.
这就是我所考虑的:使用像这里的ManualResetEvents:http://msdn.microsoft.com/en-us/library/3dasc8as%28VS.80%29.aspx但我遇到的问题是你只能等待64次或更少的事件.我可能有更多的图像.
第二个选项:有一个计数器来计算已完成的图像,并在计数达到总数时引发事件:
public void DownsizeAsync(string[] files, string destination)
{
foreach (var name in files)
{
string temp = name; //countering the closure issue
ThreadPool.QueueUserWorkItem(f =>
{
string newFileName = this.DownsizeImage(temp, destination);
this.OnImageResized(newFileName);
total++;
if (total == files.Length)
{
this.OnDownsizeCompleted(new …Run Code Online (Sandbox Code Playgroud) 我是一个控制台数独求解器,其主要目标是原始速度.
我现在有一个ManagerThread,它启动WorkerThreads来计算每个单元格的neibhbors.因此,现在为每个单元启动一个WorkerThread.如何重新使用已完成其工作的现有线程?
线程池模式似乎是解决方案,但我不明白如何防止线程在其工作完成后死亡.
ps:我不希望这个特定任务获得太多性能,只是想在将多线程应用到更复杂的代码部分之前尝试多线程的工作原理.
谢谢
I'm trying to understand some of the basics of using POSIX pthreads. The kind of thing I need to do (eventually) is parallelize some computations, using a thread pool model. At present I want to ensure I have a very basic sense for how the POSIX pthread model works. So I'm trying to create the simplest thread pool that's general enough to do the kinds of things I want to do. There will be some shared memory, an input queue, …
我有一个ExecutorService管理一些Callables.Callables运行的任务主要是黑盒转换和数字运算.在某些条件下,正在转换的数据将会振荡并且线程将花费一个多小时来完成.为了比较,大多数线程在一分钟内完成.
已经发现,长时间运行的线程中的数据并不相关.我想打断任何运行时间超过一定时间的线程.最好的方法是什么?
我使用缓存线程池ExecutorService来运行一些异步后台任务.我已经提供了我的ThreadFactory,它将线程分发给ExecutorService(只要它需要它们).我对缓存线程池的理解是,在线程空闲60秒后,它由ExecutorService终止.
当我的线程即将被终止时,我想执行一些状态清理.实现这一目标的最佳方法是什么?ExecutorService不容易为线程的生命周期提供钩子.
我不想关闭我的ExecutorService - 对于在它们到来时运行任务非常有用.
ExecutorService executor = Executors.newCachedThreadPool(new MyThreadFactory());
// Do some work
executor.submit(new MyCallable());
// Need a way for the ExecutorService to notify me when it is about to
// terminate my thread - need to perform some cleanup
Run Code Online (Sandbox Code Playgroud)
谢谢,
Shreyas
在MVC/WebAPI环境中,我将用于InRequestScope绑定DbContext.
但是,我现在处于Console应用程序/ Windows服务/ Azure辅助角色(并不重要,只是没有Web请求范围),它会定期创建一些Tasks异步运行.我希望每个任务都有自己的DbContext,并且由于任务在自己的线程上运行,我尝试DbContext使用绑定InThreadScope.
不幸的是,我意识到任务完成时不会处理DbContext.实际发生的是,线程返回线程池,当它被分配一个新任务时,它已经有一个DbContext,所以DbContexts永远保持活着.
有没有办法InThreadScope在这里使用或我应该使用其他范围?当线程从ThreadPool中不时返回时,如何使用ThreadScope?
multithreading entity-framework dependency-injection ninject threadpool
我有一个线程池中执行的任务,这些任务共享一个可重入的读写锁。如果执行完成,这些任务将返回期货。当该锁遇到争用时,重入的读写锁将在某种条件下等待。
我正在使用的库公开了一个wait_for_any方法,以从一组任务中检索一个或多个完成的期货。但是,即使一个或多个期货已经完成,在所有期货都完成之前,wait_for_any方法也将无法返回。此外,wait_for_any方法公开一个超时参数,如果设置该参数,该参数随后将被忽略。
我的问题是我在做错什么,可能导致这种wait_for_any方法阻塞?我是否了解Python的条件等待和错误通知的实现,这些构造是否会在Python中完全阻塞每个线程?
我正在使用的库称为Futurist,由OpenStack基金会维护。这里是我使用的相关类和方法的链接:GreenThreadPoolExecutor和waiters.wait_for_any
这是ReentrantReadWriteLock:
class ReentrantReadWriteLock(object):
def __init__(self):
self._read_lock = RLock()
self._write_lock = RLock()
self._condition = Condition
self._num_readers = 0
self._wants_write = False
def read_acquire(self, blocking=True):
int_lock = False
try:
if self._read_lock.acquire(blocking):
int_lock = True
LOG.warning("read internal lock acquired")
while self._wants_write:
LOG.warning("read wants write true")
if not blocking:
LOG.warning("read non blocking")
return False
LOG.warning("read wait")
with self._condition:
self._condition.wait()
first_it = False
LOG.warning("read acquired lock")
self._num_readers += 1
return True
LOG.warning("read internal lock failed")
return False
finally: …Run Code Online (Sandbox Code Playgroud) 我正在研究任务并行库,我在某处读到TPL实际上使用了CLR-Level的线程池机制.我找不到任何确认此信息的文章.我知道,TPL为每个线程都有任务队列,并使用一些特殊的工作窃取算法进行平衡.据我所知,它为每个处理器创建一个线程.自.NET 4以来,线程池开始使用TPL的任务对象.
我无法理解TPL如何使用线程池.线程池模式状态,工作项排队,线程池中的空闲线程从此队列中获取一个.然而,TPL将项目(任务)存储到线程队列中,并且如果需要的话,工作窃取工作......因此,完全不同.我的错误在哪里?
额外的问题:由于这是我的第一个Stack Overflow问题,我不确定它是否合适.是吗?
ThreadLocal执行期间存储在存储中的内容是否会在线程返回ThreadPool时自动清除(如预期的那样)?
在我的应用程序中,我ThreadLocal在一些执行期间放入一些数据,但如果下次使用相同的Thread,那么我在ThreadLocal存储中找到过时的数据.
threadpool ×10
java ×4
.net ×2
c# ×1
c++ ×1
future ×1
ninject ×1
performance ×1
posix ×1
pthreads ×1
python ×1
sudoku ×1
thread-local ×1
wait ×1