queue list

#include <iostream>
#include <cstdio>
#include <list>
#include <pthread.h>
#include <error.h>
#include <unistd.h>
#include <cstring>


using namespace std;


#define MAX_TASK_NUM    (5)


list<int> taskQueList;


pthread_mutex_t mutex;
pthread_cond_t cond;


// add task into task queue list
int write_task(int task)
{
    pthread_mutex_lock(&mutex);


    // add first task
    if (taskQueList.empty())
    {
        taskQueList.push_back(task);
        printf("%s::Add first task into task queue list. After add, task number = %zu.\n",
            __func__, taskQueList.size());


        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);


        return 0;
    }


    // task queue list is full
    if (MAX_TASK_NUM == taskQueList.size())
    {
        int tail_task = taskQueList.back();
        if (task <= tail_task)
        {
            printf("%s::Add task failed, for task queue list is full and current task(%d) is less or equal than the tail task(%d).\n",
                __func__, task, tail_task);


            pthread_mutex_unlock(&mutex);
            return -1;
        }
        else
        {
            // remove tail task
            taskQueList.pop_back();
            printf("%s::Task queue list is full, current task(%d) is lagger than the tail task(%d), then pop the tail task, current task number = %d.\n",
                __func__, task, tail_task, taskQueList.size());
        }
    }


    list<int>::iterator it;
    for (it = taskQueList.begin(); it != taskQueList.end(); ++it)
    {
        if (task >= *it)
        {
            taskQueList.insert(it, task);
            printf("%s::Add task success, task number = %d.\n", __func__, taskQueList.size());


            pthread_mutex_unlock(&mutex);
            pthread_cond_signal(&cond);
            return 0;
        }
    }


    taskQueList.insert(it, task);
    printf("%s::Add task success, task number = %d.\n", __func__, taskQueList.size());


    pthread_mutex_unlock(&mutex);
    pthread_cond_signal(&cond);
    return 0;
}


// remove task from task queue list
int read_task(int &task)
{
    pthread_mutex_lock(&mutex);


    while (taskQueList.empty())
    {
        printf("%s::Waiting for signal, for task queue list is empty.\n", __func__);
        pthread_cond_wait(&cond, &mutex);
    }


    task = taskQueList.front();
    taskQueList.pop_front();
    printf("%s::Get task success, task = %d.\n", __func__, task);


    pthread_mutex_unlock(&mutex);
    return 0;
}


// add(write) task to list
void *w_thread_handle(void *args)
{
    (void)args;
    pthread_detach(pthread_self());


    int i = -1;
    while (1)
    {
        ++i;
        int ret = write_task(i);
        if (0 != ret)
        {
            printf("%s::In write thread, write task error. error task = %d.\n", __func__, i);
        }


        //sleep(10);
    }
}


// delete(read) element from list
void *r_thread_handle(void *args)
{
    (void)args;
    pthread_detach(pthread_self());


    int task;
    while (1)
    {
        int ret = read_task(task);
        if (0 != ret)
        {
            printf("%s::In read thread, get task error.\n", __func__);
        }
        else
        {
            printf("%s::In read thread, get task(%d) success.\n", __func__, task);
        }


        //sleep(2);
    }
}


int main()
{
    int ret;
    pthread_t wId, rId;


    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);


    // create thread for write
    ret = pthread_create(&wId, NULL, w_thread_handle, NULL);
    if (0 != ret)
    {
        printf("%s::Create write thread failed, for %s.\n", __func__, strerror(ret));
        return -1;
    }


    // create thread for read
    ret = pthread_create(&rId, NULL, r_thread_handle, NULL);
    if (0 != ret)
    {
        printf("%s::Create read thread failed, for %s.\n", __func__, strerror(ret));
        return -1;
    }


    sleep(10000);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值