1.单个消费者和单个生产者模型
利用锁和条件变量完成代码
-
#include
-
#include
-
#include
-
#include
-
#include
-
enum{
-
CONSUMMER = 0,
-
PRODUCER
-
};
-
-
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
-
pthread_cond_t g_eat = PTHREAD_COND_INITIALIZER;
-
pthread_cond_t g_empty = PTHREAD_COND_INITIALIZER;
-
-
-
-
void *consummer(void *arg)
-
{
-
unsigned int *carrot = (unsigned int *)arg;
-
while(1)
-
{
-
while( pthread_mutex_trylock(&g_mutex) != 0)
-
{
-
printf("consummer wait!!\n");
-
}
-
while (*carrot == 0)
-
{
-
pthread_cond_signal(&g_empty);
-
pthread_cond_wait(&g_eat , &g_mutex);
-
}
-
*carrot = *carrot - 1;
-
printf("consummer eat: carrot-- %d\n",*carrot);
-
pthread_mutex_unlock(&g_mutex);
-
-
sleep(1);
-
}
-
}
-
-
void *producer(void *arg)
-
{
-
unsigned int *carrot = (unsigned int *)arg;
-
while(1)
-
{
-
while( pthread_mutex_trylock(&g_mutex) != 0)
-
{
-
printf("producer wait!\n");
-
}
-
-
while (*carrot > 0)
-
{
-
pthread_cond_wait(&g_empty , &g_mutex);
-
}
-
*carrot = *carrot + 5;
-
printf("producer produced :carrort + 5--%d\n",*carrot);
-
pthread_cond_signal(&g_eat);
-
pthread_mutex_unlock(&g_mutex);
-
-
sleep(1);
-
}
-
-
}
-
-
-
-
int main(int argc ,char *argv[])
-
{
-
unsigned int carrot = 9;
-
pthread_t pthreads[2];
-
-
if (pthread_create(&pthreads[CONSUMMER], NULL , &consummer , &carrot) != 0)
-
{
-
puts("pthread_create consummer failed!\n");
-
return 0;
-
}
-
-
if (pthread_create(&pthreads[PRODUCER], NULL , &producer , &carrot) != 0)
-
{
-
puts("pthread_create consummer failed!\n");
-
return 0;
-
}
-
-
pthread_join(pthreads[CONSUMMER],NULL);
-
pthread_join(pthreads[PRODUCER],NULL);
-
-
pthread_cond_destroy(&g_empty);
-
pthread_cond_destroy(&g_eat);
-
pthread_mutex_destroy(&g_mutex);
-
-
}
2.编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推(迅雷笔试题)
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <pthread.h>
-
-
#define MAX_THREAD 3
-
-
-
-
pthread_cond_t g_awakeup = PTHREAD_COND_INITIALIZER;
-
pthread_cond_t g_bwakeup = PTHREAD_COND_INITIALIZER;
-
pthread_cond_t g_cwakeup = PTHREAD_COND_INITIALIZER;
-
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-
enum {
-
paRun = 0,
-
pbRun,
-
pcRun
-
};
-
-
void *paWork(void *arg)
-
{
-
unsigned int *stats = (unsigned int *)arg;
-
int i = 0;
-
while(1)
-
{
-
pthread_mutex_lock(&g_mutex);
-
while (*stats != paRun) {pthread_cond_wait(&g_awakeup, &g_mutex);}
-
printf("A");
-
*stats = pbRun;
-
pthread_mutex_unlock(&g_mutex);
-
pthread_cond_signal(&g_bwakeup);
-
if (++i % 10 == 0) break;
-
}
-
-
}
-
void *pbWork(void *arg)
-
{
-
unsigned int *stats = (unsigned int *)arg;
-
int i = 0;
-
while(1)
-
{
-
pthread_mutex_lock(&g_mutex);
-
while (*stats != pbRun) {pthread_cond_wait(&g_bwakeup, &g_mutex);}
-
printf("B");
-
*stats = pcRun;
-
pthread_mutex_unlock(&g_mutex);
-
pthread_cond_signal(&g_cwakeup);
-
if (++i % 10 == 0) break;
-
}
-
-
}
-
void *pcWork(void *arg)
-
{
-
unsigned int *stats = (unsigned int *)arg;
-
int i = 0;
-
while(1)
-
{
-
pthread_mutex_lock(&g_mutex);
-
while (*stats != pcRun) {pthread_cond_wait(&g_cwakeup, &g_mutex);}
-
printf("C");
-
*stats = paRun;
-
pthread_mutex_unlock(&g_mutex);
-
pthread_cond_signal(&g_awakeup);
-
if (++i % 10 == 0) break;
-
}
-
}
-
-
-
int main(int argc ,char *argv[])
-
{
-
int i = 0;
-
unsigned int stats = paRun;
-
pthread_t pthreads[MAX_THREAD];
-
-
-
if (pthread_create(&pthreads[paRun] , NULL, &paWork, &stats ) != 0)
-
{
-
printf("pthread_create failed!\n");
-
return 0;
-
}
-
if (pthread_create(&pthreads[pbRun] , NULL, &pbWork, &stats ) != 0)
-
{
-
printf("pthread_create failed!\n");
-
return 0;
-
}
-
if (pthread_create(&pthreads[pcRun] , NULL, &pcWork, &stats ) != 0)
-
{
-
printf("pthread_create failed!\n");
-
return 0;
-
}
-
-
for (i = 0; i< MAX_THREAD; i++)
-
{
-
pthread_join(pthreads[i], NULL);
-
}
-
-
pthread_cond_destroy(&g_awakeup);
-
pthread_cond_destroy(&g_bwakeup);
-
pthread_cond_destroy(&g_cwakeup);
-
pthread_mutex_destroy(&g_mutex);
-
printf("\nGAME OVER!\n");
-
-
}
当然也可以将三个线程函数代码合成一个。
测试结果:
ABCABCABCABCABCABCABCABCABCABC
GAME OVER!
3.是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:
1)有一int型全局变量g_Flag初始值为0;
2) 在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1
3) 在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2
4) 线程序1需要在线程2退出后才能退出
5) 主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出
分析:线程1的退出条件是线程2 退出之后才能退出。
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <pthread.h>
-
-
#define NUM 2
-
-
int g_Flag = 0;
-
int count = 0;
-
-
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
-
pthread_cond_t g_cond = PTHREAD_COND_INITIALIZER;
-
-
void *R1(void *arg)
-
{
-
pthread_mutex_lock(&g_mutex);
-
puts("This is thread1!\n");
-
count++;
-
if (g_Flag == 2)
-
{
-
g_Flag = 1;
-
puts("R2 end ,So R1 end!\n");
-
pthread_mutex_unlock(&g_mutex);
-
} else if (g_Flag == 0)
-
{
-
puts("R1 waiting R2 end!\n");
-
pthread_cond_wait(&g_cond,&g_mutex);
-
puts("R1 wakeup, end!\n");
-
pthread_mutex_unlock(&g_mutex);
-
} else {
-
puts("unknow err!\n");
-
pthread_mutex_unlock(&g_mutex);
-
}
-
}
-
void *R2(void *arg)
-
{
-
pthread_mutex_lock(&g_mutex);
-
puts("This is thread2!\n");
-
count++;
-
g_Flag = 2;
-
puts("R2 end!\n");
-
pthread_mutex_unlock(&g_mutex);
-
pthread_cond_signal(&g_cond);
-
}
-
-
int main(int argc, char *argv[])
-
{
-
-
int i = 0;
-
pthread_t pthreads[2];
-
pthread_attr_t attr;
-
pthread_attr_init(&attr);
-
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
if (pthread_create(&pthreads[0], &attr,&R1, NULL) != 0)
{
puts("pthread_create failed! 1\n");
return 0;
}
pthread_detach(pthreads[0]);
if (pthread_create(&pthreads[1], &attr,&R2, NULL)!= 0)
{
puts("pthread_create failed! 2\n");
return 0;
}
pthread_detach(pthreads[1]);
while(1)
{
pthread_mutex_lock(&g_mutex);
if (count > 1) {pthread_mutex_unlock(&g_mutex);break;}
pthread_mutex_unlock(&g_mutex);
}
puts("main pthread over!\n");
pthread_cond_destroy(&g_cond);
pthread_mutex_destroy(&g_mutex);
pthread_attr_destroy(&attr);
}
阅读(761) | 评论(0) | 转发(0) |