当前位置: 技术问答>linux和unix
(菜鸟问题)临界区问题
来源: 互联网 发布时间:2016-07-04
本文导语: struct test { int x; int flag; //读写标志,判断是否被读过 } deque ideque; //线程1 while(cin>>i) { test *pi = new test; pi->x = i; pi->flag = 0; /...
struct test
{
int x;
int flag; //读写标志,判断是否被读过
}
deque ideque;
//线程1
while(cin>>i)
{
test *pi = new test;
pi->x = i;
pi->flag = 0; //0表示没有读过
pthread_mutex_lock(&mutex);
ideque.push_back(pi);
pthread_mutex_unlock(&mutex);
}
//线程2
deque::iterator red = ideque.begin();
while(red != ideque.end())
{
pthread_mutex_lock(&mutex); //问题 这里需要加临界吗?
cout
{
int x;
int flag; //读写标志,判断是否被读过
}
deque ideque;
//线程1
while(cin>>i)
{
test *pi = new test;
pi->x = i;
pi->flag = 0; //0表示没有读过
pthread_mutex_lock(&mutex);
ideque.push_back(pi);
pthread_mutex_unlock(&mutex);
}
//线程2
deque::iterator red = ideque.begin();
while(red != ideque.end())
{
pthread_mutex_lock(&mutex); //问题 这里需要加临界吗?
cout