不用信号量机制能否实现生产这消费问题

chengyanrong 2012-10-18 05:37:32
有两个线程,一个负责读数据,另一个负责写数据
写线程:
data[10]//读写访问的数据区域
Empyt[10]//标记数据块
if(Empty[writeIndex]){
//写数据到data[writeindex]块中;
Empty[writeindex]=false;
writeindex=(writeindex+1)%10
}
读线程
if(!Empty[readIndex]){
//读data[readindex];
Empty[readindex]=true;
readindex=(eadindex+1)%10
}
能否实现同步

...全文
152 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
傻X 2012-10-21
  • 打赏
  • 举报
回复
明确的告诉楼主:

你if要改成while。这种方式不推荐啊。因为占用系统资源太多了。不断的轮询

一半还是用4大互斥

CEvent CMutex CriticalSection(仅线程级) Semaphore

我用CEvent较多。但是这个例子CriticalSection比较适合
I_ask_who 2012-10-20
  • 打赏
  • 举报
回复
他山之石:
15.2.1. Compare and Swap
The approach taken by most processor architectures, including IA32 and Sparc, is to implement a compare-and-swap (CAS) instruction. (Other processors, such as PowerPC, implement the same functionality with a pair of instructions: loadlinked and store-conditional.) CAS has three operandsa memory location V on which to operate, the expected old value A, and the newvalue B. CAS atomically updates V to the new value B, but only if the value in V matches the expected old value A; otherwise it does nothing. In either case, it returns the value currently in V. (The variant called compare-and-set instead returns whether the operation succeeded.) CAS means "I think V should have the value A; if it does, put B there, otherwise don't change it but tell me I was wrong." CAS is an optimistic techniqueit proceeds with the update in the hope of success, and can detect failure if another thread has updated the variable since it was last examined. SimulatedCAS in Listing 15.1 illustrates the semantics (but not the implementation or performance) of CAS.

When multiple threads attempt to update the same variable simultaneously using CAS, one wins and updates the variable's value, and the rest lose. But the losers are not punished by suspension, as they could be if they failed to acquire a lock; instead, they are told that they didn't win the race this time but can try again. Because a thread that loses a CAS is not blocked, it can decide whether it wants to try again, take some other recovery action, or do nothing.[3] This flexibility eliminates many of the liveness hazards associated with locking (though in unusual cases can introduce the risk of livelocksee Section 10.3.3).

[3] Doing nothing may be a perfectly sensible response to a failed CAS; in some nonblocking algorithms, such as the linked queue algorithm in Section 15.4.2, a failed CAS means that someone else already did the work you were planning to do.
bsnry 2012-10-20
  • 打赏
  • 举报
回复
可以啊,SRWLOCK 和条件变量 很方便

不过必须是vista才支持

xp不支持啊

icessl 2012-10-20
  • 打赏
  • 举报
回复
数据区 data[10];
写指针初值 int writeIndex=0;
读指针初值 int readIndex=0;

取数据个数
int GetDataCount()
{
return (writeIndex>=readIndex) ? writeIndex-readIndex ? writeIndex+10-readIndex;
}

写线程:
while (GetDataCount()>=9) Sleep(10); // 数据没有取走,等待
写数据到data[writeindex]块中;
writeindex=(writeindex+1)%10

读线程
while (GetDataCount()==0) Sleep(10); // 没有数据,等待
读data[readindex];
readindex=(readindex+1)%10;

I_ask_who 2012-10-20
  • 打赏
  • 举报
回复
还有一个叫做Nonblocking Synchronization的东西,在java里有使用,利用CPU的一个属性,原理就是while(xxx),在超高负载情况下比信号量好
I_ask_who 2012-10-20
  • 打赏
  • 举报
回复
可以while(xxx),不过cpu负载很高的
Daisy__Ben 2012-10-20
  • 打赏
  • 举报
回复
这是很经典的问题,有很多方法实现生产消费者的同步的方法。关键是要有一个能挂起和唤醒等待线程的队列

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧