#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;
}
#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;
}