manch1n 2019-05-12 01:00 采纳率: 0%
浏览 384

关于pthread_mutex多个线程忙等待问题?

文档说如果对一个已加锁的mutex再次加锁pthread_mutex_lock会形成一个等待队列。可是这个示例有点奇怪,我知道线程没有放弃cpu,可是为什么后面输出的hello不到数量?

#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <pthread.h>
#include <errno.h>
#include <iostream>
#include <unistd.h>
#include <queue>
class sample
{
public:
    void process();
};

template <typename T>
class threadPool
{
public:
    threadPool();
    void append(T &s);
    static void *worker(void *s);

private:
    std::queue<T *> tasks;
    pthread_t threads[5];
    bool run = true;
    pthread_mutex_t t;
};

// template <typename T>
// void threadPool<T>::append(T& s)
// {
//     pthread_t t;
//     pthread_create(&t,nullptr,worker,&s);
//     pthread_detach(t);
// }
template <typename T>
threadPool<T>::threadPool() : tasks()
{
    pthread_mutex_init(&t,nullptr);
    for(int i=0;i<5;++i)
    {
        pthread_create(&threads[i],nullptr,worker,this);
        pthread_detach(threads[i]);
    }
}

template <typename T>
void threadPool<T>::append(T &s)
{
    pthread_mutex_lock(&t);
    tasks.push(&s);
    printf("%d append.\n",pthread_self());
    pthread_mutex_unlock(&t);
}

template <typename T>
void *threadPool<T>::worker(void *s)
{
    auto pool=static_cast<threadPool<T>*>(s);
    while (pool->run)
    {
        pthread_mutex_lock(&(pool->t));
        if (pool->tasks.empty())
        {
            pthread_mutex_unlock(&(pool->t));
            printf("%d get the lock but continue.\n",pthread_self());
            continue;
        }
        auto work = pool->tasks.front();
        pool->tasks.pop();
        printf("%d get the lock and print\n",pthread_self());
        pthread_mutex_unlock(&(pool->t));
        work->process();
    }
    return nullptr;
}

#endif //!THREAD_POOL_H
#include "thread_pool.h"

void sample::process()
{
    std::cout<<"hello world.\n"<<std::endl;
}
#include "thread_pool.h"
#include <iostream>

int main(int argc, char **argv)
{
    threadPool<sample> pool;
    sample d[100];
    for(int i=0;i<100;++i)
    {
        pool.append(d[i]);
    }
    return 0;
}
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1268307712 get the lock but continue.
1285158720 append.
1268307712 get the lock and print
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1243129600 get the lock but continue.
1259915008 get the lock but continue.
1251522304 get the lock but continue.
hello world.
1285158720 append.

1268307712 get the lock and print
hello world.

1268307712 get the lock but continue.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1251522304 get the lock and print
hello world.

1251522304 get the lock and print
hello world.
1259915008 get the lock and print

1243129600 get the lock and print
hello world.

1243129600 get the lock and print
hello world.

1243129600 get the lock and print
hello world.

1243129600 get the lock and print
hello world.

1243129600 get the lock and print
hello world.
1285158720 append.
1251522304 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1285158720 append.
1234736896 get the lock and print
hello world.

1259915008 get the lock and print
hello world.

1259915008 get the lock and print
hello world.

1259915008 get the lock and print
hello world.
hello world.



1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1251522304 get the lock and print
hello world.

1251522304 get the lock and print
hello world.

1251522304 get the lock and print
hello world.

1251522304 get the lock and print
hello world.

1251522304 get the lock and print
hello world.

1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1251522304 get the lock but continue.
1268307712 get the lock but continue.
1259915008 get the lock but continue.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1234736896 get the lock and print
hello world.

1234736896 get the lock and print
hello world.

1243129600 get the lock and print
hello world.

1243129600 get the lock and print
hello world.

1243129600 get the lock and print
hello world.

1243129600 get the lock and print
hello world.

1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1243129600 get the lock but continue.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.
1285158720 append.

头晕了一下午,麻烦大佬出来解救众生。

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-09 16:30
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型