互斥cond等待类错误或错误

时间:2021-11-21 00:33:40

I have problem with this simple wrapper of mutex / cond wait class. I use it to protect queue of connected clients and to notify threads in the wait state that there are new clients connected, but application crashed with Segmentation Failed at

我有这个简单的mutex / cond等待包装器的问题。我使用它来保护连接客户端的队列,并通知处于等待状态的线程已连接新客户端,但应用程序崩溃,分段失败,

client_socket_fd = clients_for_threads_q.front();

when its is under stress test only (many clients connected )

当它仅在压力测试下(许多客户连接)

Wait at the thread:

等待线程:

    MUTEXCONDSIGNAL_for_workers.lock();
    if ( clients_for_threads_q.empty() )
    {
        MUTEXCONDSIGNAL_for_workers.wait_signal();
    }

    client_socket_fd = clients_for_threads_q.front(); // CRASHED !!!!
    clients_for_threads_q.pop();
    MUTEXCONDSIGNAL_for_workers.unlock();

of course clients_for_threads_q ptotected on writing

当然,clients_for_threads_q在写作时受到了保护

    MUTEXCONDSIGNAL_for_workers.lock();
    clients_for_threads_q.push (client_socket_fd);
    MUTEXCONDSIGNAL_for_workers.send_signal();
    MUTEXCONDSIGNAL_for_workers.unlock();

1 个解决方案

#1


4  

pthread_cond_wait can have spurious wake ups.

pthread_cond_wait会有虚假的唤醒。

When using condition variables there is always a Boolean predicate involving shared variables associated with each condition wait that is true if the thread should proceed. Spurious wakeups from the pthread_cond_wait() or pthread_cond_timedwait() functions may occur. Since the return from pthread_cond_wait() or pthread_cond_timedwait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.

使用条件变量时,总会有一个布尔谓词,涉及与每个条件等待关联的共享变量,如果线程应该继续,则为true。可能会发生pthread_cond_wait()或pthread_cond_timedwait()函数的虚假唤醒。由于从pthread_cond_wait()或pthread_cond_timedwait()返回并不意味着有关此谓词的值的任何内容,因此应在返回时重新评估谓词。

You need to change your waiter if condition to a loop:

如果条件为循环,您需要更改服务员:

while ( clients_for_threads_q.empty() )
{
    MUTEXCONDSIGNAL_for_workers.wait_signal();
}

#1


4  

pthread_cond_wait can have spurious wake ups.

pthread_cond_wait会有虚假的唤醒。

When using condition variables there is always a Boolean predicate involving shared variables associated with each condition wait that is true if the thread should proceed. Spurious wakeups from the pthread_cond_wait() or pthread_cond_timedwait() functions may occur. Since the return from pthread_cond_wait() or pthread_cond_timedwait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.

使用条件变量时,总会有一个布尔谓词,涉及与每个条件等待关联的共享变量,如果线程应该继续,则为true。可能会发生pthread_cond_wait()或pthread_cond_timedwait()函数的虚假唤醒。由于从pthread_cond_wait()或pthread_cond_timedwait()返回并不意味着有关此谓词的值的任何内容,因此应在返回时重新评估谓词。

You need to change your waiter if condition to a loop:

如果条件为循环,您需要更改服务员:

while ( clients_for_threads_q.empty() )
{
    MUTEXCONDSIGNAL_for_workers.wait_signal();
}