首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
写一个类 A,它包含一个长度为 10 的整型数组,一个函数,
[问答题]
写一个类 A,它包含一个长度为 10 的整型数组,一个函数,可以插入数据到数组尾,另一个函数,可以取 得数组的第一个数据。写一个类,调用 A 的方法插入数据。写一个类,调用 A 的方法得到数据。要求使用多线程。
添加笔记
求解答(9)
邀请回答
收藏(34)
分享
纠错
1个回答
添加回答
0
BruceM
#include "pthread.h" #include "unistd.h" #include <iostream> using namespace std; #define SIZE 10 class A{ private: int arry[SIZE]; int pos; pthread_mutex_t mutex; pthread_cond_t notfull; pthread_cond_t notempty; public: A(); ~A(); int getLast(); void setFirst(int element); }; A::A(): pos(0){ pthread_mutex_init(&mutex, NULL); pthread_cond_init(¬full, NULL); pthread_cond_init(¬empty, NULL); } A::~A(){ pthread_mutex_destory(&mutex); pthread_cond_destory(¬full); pthread_cond_destory(¬empty); } int A::getLast(){ int returntmp; pthread_mutex_lock(&mutex); if(pos < 1) pthread_cond_wait(¬empty, &mutex); returntmp = arry[pos]; pos--; pthread_cond_signal(¬full); pthread_mutex_unlock(&mutex); return returntmp; } void A::setFirst(int element){ pthread_mutex_lock(&mutex); if(pos > SIZE - 1) pthread_cond_wait(¬full, &mutex); for(int i = pos+1;i > 0;--i) arry[i] = arry[--i]; arry[0] = element; pos++; pthread_cond_signal(¬empty); pthread_mutex_unlock(&mutex); } A a; void *put(int n) { for(int i = 0;i < SIZE; ++i) a.setFirst(n); } void *get() { for(int i = 0;i < SIZE; ++i) cout<<a.getLast(); cout<<endl; } int main(){ pthread_t pt1, pt2; pthread_create(&pt1, NULL, put, 0); pthread_create(&pt2, NULL, get, 0); return 0; }
发表于 2016-06-10 10:17:10
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
数组
并发
华为
上传者:
磐石
难度:
1条回答
34收藏
7480浏览
热门推荐
相关试题
无锁化编程有哪些常见方法?
阿里巴巴
并发
评论
(39)
来自
阿里巴巴2015实习生笔试题
设某二叉树的先序遍历序列为abdg...
树
评论
(1)
之前的经历中单品数据分析的经验丰富...
评论
(1)
什么样的人适合做数据分析
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题