6

デッドロックが発生していると思われるpthreadに問題があります。動作していると思ったブロッキングキューを作成しましたが、さらにテストを行った後、blocking_queueでブロックしている複数のスレッドをキャンセルしようとすると、デッドロックが発生するようです。

ブロッキングキューは非常に単純で、次のようになります。

template <class T> class Blocking_Queue
{
public:
    Blocking_Queue()
    {
        pthread_mutex_init(&_lock, NULL);
        pthread_cond_init(&_cond, NULL);
    }

    ~Blocking_Queue()
    {
        pthread_mutex_destroy(&_lock);
        pthread_cond_destroy(&_cond);
    }

    void put(T t)
    {
        pthread_mutex_lock(&_lock);
        _queue.push(t);
        pthread_cond_signal(&_cond);
        pthread_mutex_unlock(&_lock);
    }

     T pull()
     {
        pthread_mutex_lock(&_lock);
        while(_queue.empty())
        {
            pthread_cond_wait(&_cond, &_lock);
        }

        T t = _queue.front();
        _queue.pop();

        pthread_mutex_unlock(&_lock);

        return t;
     }

priavte:
    std::queue<T> _queue;
    pthread_cond_t _cond;
    pthread_mutex_t _lock;
}

テストのために、このブロッキングキューをプルする4つのスレッドを作成しました。いくつかのprintステートメントをブロッキングキューに追加しました。各スレッドはpthread_cond_wait()メソッドに到達しています。ただし、各スレッドでpthread_cancel()とpthread_join()を呼び出そうとすると、プログラムがハングします。

また、これを1つのスレッドでテストしたところ、完全に機能します。

ドキュメントによると、pthread_cond_wait()はキャンセルポイントであるため、これらのスレッドでcancelを呼び出すと、実行が停止するはずです(これは、1つのスレッドでのみ機能します)。ただし、pthread_mutex_lockはキャンセルポイントではありません。pthread_cancel()が呼び出され、キャンセルされたスレッドが終了する前にミューテックスを取得してロックを解除し、次のスレッドがキャンセルされたときにミューテックスとデッドロックを取得できないという状況で何かが起こっている可能性がありますか?または、私が間違っていることは他にありますか。

どんなアドバイスも素敵でしょう。ありがとう :)

4

3 に答える 3

5

pthread_cancel()避けるのが最善です。

Blocking_Queue :: pull()でブロックされているすべてのスレッドのブロックを解除するには、そこから例外をスローします。

キューの弱点の1つはT t = _queue.front();、例外をスローする可能性のあるTのコピーコンストラクターを呼び出して、キューのミューテックスを永久にロックすることです。C++スコープのロックを使用することをお勧めします。

正常なスレッド終了の例を次に示します。

$ cat test.cc
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <exception>
#include <list>
#include <stdio.h>

struct BlockingQueueTerminate
    : std::exception
{};

template<class T>
class BlockingQueue
{
private:
    boost::mutex mtx_;
    boost::condition_variable cnd_;
    std::list<T> q_;
    unsigned blocked_;
    bool stop_;

public:
    BlockingQueue()
        : blocked_()
        , stop_()
    {}

    ~BlockingQueue()
    {
        this->stop(true);
    }

    void stop(bool wait)
    {
        // tell threads blocked on BlockingQueue::pull() to leave
        boost::mutex::scoped_lock lock(mtx_);
        stop_ = true;
        cnd_.notify_all();

        if(wait) // wait till all threads blocked on the queue leave BlockingQueue::pull()
            while(blocked_)
                cnd_.wait(lock);
    }

    void put(T t)
    {
        boost::mutex::scoped_lock lock(mtx_);
        q_.push_back(t);
        cnd_.notify_one();
    }

    T pull()
    {
        boost::mutex::scoped_lock lock(mtx_);

        ++blocked_;
        while(!stop_ && q_.empty())
            cnd_.wait(lock);
        --blocked_;

        if(stop_) {
            cnd_.notify_all(); // tell stop() this thread has left
            throw BlockingQueueTerminate();
        }

        T front = q_.front();
        q_.pop_front();
        return front;
    }
};

void sleep_ms(unsigned ms)
{
    // i am using old boost
    boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(ms));
    // with latest one you can do this
    //boost::thread::sleep(boost::posix_time::milliseconds(10));
}

void thread(int n, BlockingQueue<int>* q)
try
{
    for(;;) {
        int m = q->pull();
        printf("thread %u: pulled %d\n", n, m);
        sleep_ms(10);
    }
}
catch(BlockingQueueTerminate&)
{
    printf("thread %u: finished\n", n);
}

int main()
{
    BlockingQueue<int> q;

    // create two threads
    boost::thread_group tg;
    tg.create_thread(boost::bind(thread, 1, &q));
    tg.create_thread(boost::bind(thread, 2, &q));
    for(int i = 1; i < 10; ++i)
        q.put(i);
    sleep_ms(100); // let the threads do something
    q.stop(false); // tell the threads to stop
    tg.join_all(); // wait till they stop
}

$ g++ -pthread -Wall -Wextra -o test -lboost_thread-mt test.cc

$ ./test
thread 2: pulled 1
thread 1: pulled 2
thread 1: pulled 3
thread 2: pulled 4
thread 1: pulled 5
thread 2: pulled 6
thread 1: pulled 7
thread 2: pulled 8
thread 1: pulled 9
thread 2: finished
thread 1: finished
于 2011-02-16T16:55:15.617 に答える
1

私はpthread_cancel()に精通していません-協調終了を好みます。

pthread_cancel()は、ミューテックスをロックしたままにしませんか?キャンセルハンドラーでクリーンアップする必要があると思います。

于 2011-02-16T16:12:39.597 に答える
1

私はpthread_cond_wait()/ pthread_cancel()で同様の経験をしました。なんらかの理由でスレッドが戻った後もロックが保持されているという問題があり、ロックしたのと同じスレッドでロックを解除する必要があるため、ロックを解除できませんでした。デッドロックが発生しなかった単一のプロデューサー、単一のコンシューマーの状況があったため、pthread_mutex_destroy()を実行しているときにこれらのエラーに気づきました。

pthread_cond_wait()は、戻るときにミューテックスをロックすることになっています。これが発生した可能性がありますが、スレッドを強制的にキャンセルしたため、最後のロック解除は行われませんでした。一部のプラットフォームはこれをサポートしていないため、安全のために、私は通常、pthread_cancel()の使用を完全に避けようとします。揮発性のブール値またはアトミックを使用して、スレッドをシャットダウンする必要があるかどうかを確認できます。そうすれば、ミューテックスもクリーンに処理されます。

于 2011-02-16T16:17:09.610 に答える