标签: multithreading

BackgroundWorker中未处理的异常

我的WinForms应用程序使用许多BackgroundWorker对象从数据库中检索信息.我正在使用BackgroundWorker,因为它允许UI在长时间运行的数据库查询期间保持解除阻塞状态,并简化了我的线程模型.

我在其中一些后台线程中偶尔会遇到DatabaseExceptions,并且在调试时我在工作线程中至少目睹了其中一个异常.我相当有信心这些例外是超时,我认为它不时是合理的.

我的问题是当在其中一个后台工作线程中发生未处理的异常时会发生什么.

我不认为我可以在另一个线程中捕获异常,但我可以期望我的WorkerCompleted方法被执行吗?我是否可以查询异常的BackgroundWorker的任何属性或方法?

.net c# debugging multithreading backgroundworker

71
推荐指数
3
解决办法
3万
查看次数

在Java关键部分中,我应该同步什么?

在Java中,在代码中声明关键部分的惯用方法如下:

private void doSomething() {
  // thread-safe code
  synchronized(this) {
    // thread-unsafe code
  }
  // thread-safe code
}
Run Code Online (Sandbox Code Playgroud)

几乎所有的块都同步 this,但是有什么特别的原因吗?还有其他可能吗?是否有关于同步对象的最佳实践?(比如私人实例Object?)

java multithreading synchronized

70
推荐指数
5
解决办法
4万
查看次数

在没有活动异常的情况下调用C++终止

我通过线程获得C++错误:

terminate called without an active exception
Aborted
Run Code Online (Sandbox Code Playgroud)

这是代码:

#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>

template<typename TYPE>
class blocking_stream
{
public:
    blocking_stream(size_t max_buffer_size_)
        :   max_buffer_size(max_buffer_size_)   
    {
    }

    //PUSH data into the buffer
    blocking_stream &operator<<(TYPE &other)
    {
        std::unique_lock<std::mutex> mtx_lock(mtx); 
        while(buffer.size()>=max_buffer_size)
            stop_if_full.wait(mtx_lock);

        buffer.push(std::move(other));

        mtx_lock.unlock();
        stop_if_empty.notify_one();
        return *this;
    }
    //POP data out of the buffer 
    blocking_stream &operator>>(TYPE &other)
    {
        std::unique_lock<std::mutex> mtx_lock(mtx);
        while(buffer.empty())
            stop_if_empty.wait(mtx_lock);

        other.swap(buffer.front()); 
        buffer.pop();

        mtx_lock.unlock();
        stop_if_full.notify_one();
        return *this;
    }

private:
    size_t max_buffer_size;
    std::queue<TYPE> buffer;
    std::mutex mtx;
    std::condition_variable stop_if_empty,
                            stop_if_full;
    bool …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading deadlock c++11

70
推荐指数
3
解决办法
9万
查看次数

SpinWait与睡眠等待.哪一个使用?

是否有效率

SpinWait.SpinUntil(() => myPredicate(), 10000)
Run Code Online (Sandbox Code Playgroud)

超时10000毫秒

要么

Thread.Sleep在相同条件下使用轮询是否更有效例如,以下SleepWait函数的行:

public bool SleepWait(int timeOut)
{
    Stopwatch stopwatch = new Stopwatch(); 
    stopwatch.Start();
    while (!myPredicate() && stopwatch.ElapsedMilliseconds < timeOut)
    {
       Thread.Sleep(50)
    }  
    return myPredicate()
}
Run Code Online (Sandbox Code Playgroud)

我担心如果我们谈论超过1秒的超时,SpinWait的所有收益可能都不是一个好的使用模式?这是一个有效的假设吗?

您更喜欢哪种方法?为什么?还有另一种更好的方法吗?


更新 - 变得更具体:

有没有办法让BlockingCollection Pulse在达到有界容量时成为睡眠线程?正如Marc Gravel建议的那样,我宁愿避免忙碌等待.

c# multithreading

70
推荐指数
2
解决办法
6万
查看次数

如何在不使用<mutex>的情况下在C++ 11中实现多线程安全单例

既然C++ 11有多线程,我想知道在不使用互斥锁的情况下实现延迟初始化单例的正确方法是什么(出于性能原因).我想出了这个,但是我并不擅长编写无锁代码,所以我正在寻找更好的解决方案.

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
# include <atomic>
# include <thread>
# include <string>
# include <iostream>
using namespace std;
class Singleton
{

public:
    Singleton()
    {
    }
static  bool isInitialized()
    {
        return (flag==2);
    }
static  bool initizalize(const string& name_)
    {
        if (flag==2)
            return false;// already initialized
        if (flag==1)
            return false;//somebody else is initializing
        if (flag==0)
        {
            int exp=0;
            int desr=1;
            //bool atomic_compare_exchange_strong(std::atomic<T>* obj, T* exp, T desr)
            bool willInitialize=std::atomic_compare_exchange_strong(&flag, &exp, desr); …
Run Code Online (Sandbox Code Playgroud)

c++ singleton multithreading atomic c++11

70
推荐指数
3
解决办法
7万
查看次数

C++ 11:为什么std :: condition_variable使用std :: unique_lock?

我对std::unique_lock工作时的作用感到有些困惑std::condition_variable.据我理解的文档,std::unique_lock基本上是一个臃肿锁定挡板,以交换两个锁之间的状态的可能性.

到目前为止我已经pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)用于此目的(我猜这是STL在posix上使用的).它需要一个互斥锁,而不是锁.

这有什么区别?std::condition_variable处理std::unique_lock优化的事实是什么?如果是这样,它究竟是如何更快?

c++ multithreading mutex c++11

70
推荐指数
2
解决办法
2万
查看次数

并发JUnit测试

我有一个大的JUnit测试套件,我非常希望同时运行所有测试,原因有两个:

  • 利用多个内核来更快地运行整个测试套件
  • 希望检测由于非线程安全的全局对象导致的一些错误

我认识到这将迫使我重构一些代码以使其成为线程安全的,但我认为这是一件好事:-)

让JUnit同时运行所有测试的最佳方法是什么?

java concurrency junit multithreading unit-testing

69
推荐指数
4
解决办法
7万
查看次数

获取std :: future的状态

是否可以检查a std::future是否已经完成?据我所知,唯一的方法就是调用wait_for零持续时间并检查状态是否ready存在,但是有更好的方法吗?

c++ multithreading future c++11

69
推荐指数
3
解决办法
3万
查看次数

如何获取异步调用的JNI接口指针(JNIEnv*)

我了解到JNI接口指针(JNIEnv*)仅在当前线程中有效.假设我在本机方法中启动了一个新线程; 它如何异步发送事件到Java方法?由于这个新线程不能有(JNIEnv*)的引用.为(JNIEnv*)存储全局变量显然不起作用?

c java java-native-interface multithreading

69
推荐指数
2
解决办法
6万
查看次数

为什么OpenMP不允许使用!=运算符?

我试图编译以下代码:

#pragma omp parallel shared (j)
{
   #pragma omp for schedule(dynamic)
   for(i = 0; i != j; i++)
   {
    // do something
   }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:错误:控制谓词无效.

我检查了openMP 参考指南,它说对于它的并行"仅"允许以下运算符之一:<=>> =.

我不明白为什么不允许i != j.我可以理解它是否是静态调度,因为openMP需要预先计算分配给每个线程的迭代次数.但我无法理解为什么在这种情况下这种限制为例.有线索吗?


编辑:即使我做for(i = 0; i != 100; i++),虽然我可以把"<"或"<=".

c c++ parallel-processing multithreading openmp

69
推荐指数
3
解决办法
5515
查看次数