/* Copy right(c) 2014
* All rights reserved
* http://blog.csdn.net/ezhou_liukai
*/
#include
#include
#include
#include
const int QUEUE_LEN = 10; //Length of circular queue
int g_nArrQueue[QUEUE_LEN]; //circular queue
int g_ni, g_nj;
int g_nDataR, g_nDataW;
sem_t g_sem_full, g_sem_empty;
pthread_mutex_t g_mutex_reader, g_mutex_writer;
void *ReaderThreadFun(void *arg)
{
printf("The ID:%u of reader starts to read the datas\n", (unsigned int)pthread_self());
int nDataR = 0;
while (nDataR < 20)
{
sem_wait(&g_sem_full);
nDataR = g_nArrQueue[g_ni];
g_ni = (g_ni + 1)%QUEUE_LEN;
printf("The ID:%u of reader reads the data:%d from circular queue\n", (unsigned int)pthread_self(), nDataR);
sem_post(&g_sem_empty);
}
printf("The ID:%u of reader ends to read the datas\n", (unsigned int)pthread_self());
}
void *MultiReaderThreadFun(void *arg)
{
printf("The ID:%u of reader starts to read the datas\n", (unsigned int)pthread_self());
while (g_nDataR < 20)
{
pthread_mutex_lock(&g_mutex_reader);
if (g_nDataR >= 20)
{
pthread_mutex_unlock(&g_mutex_reader);
printf("The ID:%u of reader ends to read the datas\n", (unsigned int)pthread_self());
return NULL;
}
sem_wait(&g_sem_full);
sleep(1);
g_nDataR = g_nArrQueue[g_ni];
g_ni = (g_ni + 1)%QUEUE_LEN;
printf("The ID:%u of reader reads the data:%d from circular queue\n", (unsigned int)pthread_self(), g_nDataR);
sem_post(&g_sem_empty);
pthread_mutex_unlock(&g_mutex_reader);
}
printf("The ID:%u of reader ends to read the datas\n", (unsigned int)pthread_self());
}
void *WriterThreadFun(void *arg)
{
printf("The ID:%u of writer starts to write the datas\n", (unsigned int)pthread_self());
int nDataW = 0;
while (nDataW < 20)
{
sem_wait(&g_sem_empty);
g_nArrQueue[g_nj] = ++nDataW;
g_nj = (g_nj + 1)%QUEUE_LEN;
printf("The ID:%u writer writes the data:%d to circular queue\n", (unsigned int)pthread_self(), nDataW);
sem_post(&g_sem_full);
}
printf("The ID:%u of writer ends to write the datas\n", (unsigned int)pthread_self());
}
void *MultiWriterThreadFun(void *arg)
{
printf("The ID:%u of writer starts to write the datas\n", (unsigned int)pthread_self());
while (g_nDataW < 20)
{
pthread_mutex_lock(&g_mutex_writer);
if (g_nDataW >= 20)
{
pthread_mutex_unlock(&g_mutex_writer);
printf("The ID:%u of writer ends to write the datas\n", (unsigned int)pthread_self());
return NULL;
}
sem_wait(&g_sem_empty);
sleep(1);
g_nArrQueue[g_nj] = ++g_nDataW;
g_nj = (g_nj + 1)%QUEUE_LEN;
printf("The ID:%u writer writes the data:%d to circular queue\n", (unsigned int)pthread_self(), g_nDataW);
sem_post(&g_sem_full);
pthread_mutex_unlock(&g_mutex_writer);
}
printf("The ID:%u of writer ends to write the datas\n", (unsigned int)pthread_self());
}
void SingleReaderSingleWriter()
{
printf("\n----------- Simulating one reader and one writer using circular queue -----------\n");
pthread_t tid_reader, tid_writer;
g_ni = g_nj = 0;
sem_init(&g_sem_empty, 0, 10);
sem_init(&g_sem_full, 0, 0);
pthread_create(&tid_reader, NULL, ReaderThreadFun, NULL);
pthread_create(&tid_writer, NULL, WriterThreadFun, NULL);
pthread_join(tid_reader, NULL);
pthread_join(tid_writer, NULL);
printf("//----------- end -----------\n\n\n");
}
void MultiReaderSingleWriter()
{
printf("\n----------- Simulating ten readers and one writer using circular queue -----------\n");
pthread_t tid_reader[10], tid_writer;
g_ni = g_nj = 0;
g_nDataR = g_nDataW = 0;
sem_init(&g_sem_empty, 0, 10);
sem_init(&g_sem_full, 0, 0);
pthread_mutex_init(&g_mutex_reader, NULL);
int i = 0;
for (; i < 10; i++)
{
pthread_create(&tid_reader[i], NULL, MultiReaderThreadFun, NULL);
}
pthread_create(&tid_writer, NULL, WriterThreadFun, NULL);
for (i = 0; i < 10; i++)
{
pthread_join(tid_reader[i], NULL);
}
pthread_join(tid_writer, NULL);
printf("//----------- end -----------\n\n\n");
}
void MultiReaderMultiWriter()
{
printf("\n----------- Simulating ten readers and ten writers using circular queue -----------\n");
pthread_t tid_reader[10], tid_writer[10];
g_ni = g_nj = 0;
g_nDataR = g_nDataW = 0;
sem_init(&g_sem_empty, 0, 10);
sem_init(&g_sem_full, 0, 0);
pthread_mutex_init(&g_mutex_reader, NULL);
pthread_mutex_init(&g_mutex_writer, NULL);
int i = 0;
for (; i < 10; i++)
{
pthread_create(&tid_reader[i], NULL, MultiReaderThreadFun, NULL);
pthread_create(&tid_writer[i], NULL, MultiWriterThreadFun, NULL);
}
for (i = 0; i < 10; i++)
{
pthread_join(tid_reader[i], NULL);
pthread_join(tid_writer[i], NULL);
}
printf("//----------- end ----------\n\n\n");
}
void *MultiThreadTest(void *arg)
{
int data = 0;
while (data < 20)
{
data++;
printf("%u entry in \n", (unsigned int)pthread_self());
}
}
void Test()
{
pthread_mutex_init(&g_mutex_reader, NULL);
pthread_t tid[10];
int i = 0;
for (; i < 10; i++)
{
pthread_create(&tid[i], NULL, MultiThreadTest, NULL);
}
for (i = 0; i < 10; i++)
{
pthread_join(tid[i], NULL);
}
}
int main()
{
SingleReaderSingleWriter();
MultiReaderSingleWriter();
MultiReaderMultiWriter();
// Test();
return 1;
}