一个条件变量的测试程序,broadcast后并不是所有线程退出,求解

小鸟向前飞 2017-11-29 10:49:10
代码如下,逻辑很简单,创建5个线程等待条件变量的broadcast,这里有两个疑惑。
1. broadcast之后并没有全部线程退出,握的系统是ubuntu14 64位
2. 一开始使用一个线程测试broadcast,发现broadcast后这个线程没有接到条件信号。

这两个问题可能是出于同一个错误吧,麻烦帮我看看代码哪里有问题。
多谢了



#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

#include <iostream>

using namespace std;


pthread_mutex_t mutex;
pthread_cond_t cond;

void* thread_func(void* param);

int main()
{
pthread_mutex_init(&mutex, nullptr);
pthread_cond_init(&cond, nullptr);

pthread_t t_id[5] = { 0 };
for (int i = 0; i < 5; i++)
{
pthread_create(&t_id[i], nullptr, thread_func, nullptr);
}

sleep(7);
pthread_cond_broadcast(&cond);
//pthread_cond_signal(&cond);

for (int i = 0; i < 5; i++)
{
pthread_join(t_id[i], nullptr);
}

return 0;
}



void* thread_func(void* param)
{
pthread_mutex_lock(&mutex);

struct timeval now;
struct timespec outtime;

while (true)
{
gettimeofday(&now, NULL);
outtime.tv_sec = now.tv_sec + 3;
outtime.tv_nsec = now.tv_usec * 1000;

int res = pthread_cond_timedwait(&cond, &mutex, &outtime);
if (res == ETIMEDOUT)
{
cout << "etime out " << pthread_self() << endl;
}
else if (res == 0)
{
cout << "res = 0." << endl;
break;
}
else
{
cout << "res = " << res << endl;
}

sleep(1);
}


cout << "thread: " + to_string(pthread_self()) + " exit." << endl;

pthread_mutex_unlock(&mutex);

return nullptr;
}


...全文
167 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-11-29
  • 打赏
  • 举报
回复
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

#include <iostream>
#include <string>

using namespace std;

#define nullptr NULL

pthread_mutex_t mutex;
pthread_cond_t cond;

void* thread_func(void* param);

int main()
{
    pthread_mutex_init(&mutex, nullptr);
    pthread_cond_init(&cond, nullptr);

    pthread_t t_id[5] = { 0 };
    for (int i = 0; i < 5; i++)
    {
        pthread_create(&t_id[i], nullptr, thread_func, nullptr);
    }

    sleep(7);
    pthread_cond_broadcast(&cond);
    //pthread_cond_signal(&cond);

    for (int i = 0; i < 5; i++)
    {
        pthread_join(t_id[i], nullptr);
    }

    return 0;
}



void* thread_func(void* param)
{

    struct timeval now;
    struct timespec outtime;

    pthread_t pid = pthread_self();
    while (true)
    {
        gettimeofday(&now, NULL);
        outtime.tv_sec = now.tv_sec + 3;
        outtime.tv_nsec = now.tv_usec * 1000;

        pthread_mutex_lock(&mutex);
        int res = pthread_cond_timedwait(&cond, &mutex, &outtime);
        if (res == ETIMEDOUT)
        {
            pthread_mutex_unlock(&mutex);
            cout << "etime out No."<<pid<<" "<< endl;
        }
        else if (res == 0)
        {
            pthread_mutex_unlock(&mutex);
            cout << "res = 0." << "No."<<pid<<endl;
            break;
        }
        else
        {
            pthread_mutex_unlock(&mutex);
            cout << "res = " << res << endl;
        }
    }
    cout << "thread: " + to_string(pid) + " exit." << endl;

    return nullptr;
}
线程不能全部退出,原因有两个,一个是锁加的的有问题,可能会导致死锁。 第二个原因,pthread_self();函数调用会占用线程调用(接收cond)时机。因此,建议pthread_self在循环外调用。 上面的代码在我的环境里可以正常运行。可以试一下,如有问题提出来;
小鸟向前飞 2017-11-29
  • 打赏
  • 举报
回复
引用 1 楼 cfjtaishan 的回复:
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

#include <iostream>
#include <string>

using namespace std;

#define nullptr NULL

pthread_mutex_t mutex;
pthread_cond_t cond;

void* thread_func(void* param);

int main()
{
    pthread_mutex_init(&mutex, nullptr);
    pthread_cond_init(&cond, nullptr);

    pthread_t t_id[5] = { 0 };
    for (int i = 0; i < 5; i++)
    {
        pthread_create(&t_id[i], nullptr, thread_func, nullptr);
    }

    sleep(7);
    pthread_cond_broadcast(&cond);
    //pthread_cond_signal(&cond);

    for (int i = 0; i < 5; i++)
    {
        pthread_join(t_id[i], nullptr);
    }

    return 0;
}



void* thread_func(void* param)
{

    struct timeval now;
    struct timespec outtime;

    pthread_t pid = pthread_self();
    while (true)
    {
        gettimeofday(&now, NULL);
        outtime.tv_sec = now.tv_sec + 3;
        outtime.tv_nsec = now.tv_usec * 1000;

        pthread_mutex_lock(&mutex);
        int res = pthread_cond_timedwait(&cond, &mutex, &outtime);
        if (res == ETIMEDOUT)
        {
            pthread_mutex_unlock(&mutex);
            cout << "etime out No."<<pid<<" "<< endl;
        }
        else if (res == 0)
        {
            pthread_mutex_unlock(&mutex);
            cout << "res = 0." << "No."<<pid<<endl;
            break;
        }
        else
        {
            pthread_mutex_unlock(&mutex);
            cout << "res = " << res << endl;
        }
    }
    cout << "thread: " + to_string(pid) + " exit." << endl;

    return nullptr;
}
线程不能全部退出,原因有两个,一个是锁加的的有问题,可能会导致死锁。 第二个原因,pthread_self();函数调用会占用线程调用(接收cond)时机。因此,建议pthread_self在循环外调用。 上面的代码在我的环境里可以正常运行。可以试一下,如有问题提出来;
多谢

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧