线程未同步 [英] threads not synchronized

查看:80
本文介绍了线程未同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果线程未针对共享内存平台编写的程序中的关键部分进行同步b>

What happens if threads not synchronized for critical sections in programs written for shared memory platforms b>

推荐答案

这会很简单:使用一些逻辑并回答你自己的问题。 关键部分未同步不是逻辑上一致的声明。由于临界区实际上是同步原语本身,在实际代码中它可能意味着a)您正确使用临界区并且线程实际上是同步的; b)您没有使用临界区;或者不要正确使用它,这可能会导致任何不可预测的行为,包括死锁。实际上,术语关键部分可能意味着两个不同的相关事物:1)实现互斥的关键部分对象; 2)使用这个原语互锁的代码部分。



你只需要了解互斥是如何工作的:

http://en.wikipedia.org/wiki/Mutual_exclusion [ ^ ],

http://en.wikipedia.org/wiki/Deadlock [ ^ ]。



请注意,互斥不是消除死锁的方法,根本不是。这是一种保持某些代码片段完整性的方法。想象一下,你有两个整数 A B ,可由两个或多个线程访问。你有一个不变量的成像: A + B == C ,其中 C 是一些常数。然后你需要提供一些原子操作来保持这种不变的情况。 (另请参阅: http://en.wikipedia.org/wiki/Atomicity_%28programming%29 [ ^ ])。你怎么能这样做?例如

This is simple: use some logic and answer your own question. "Not synchronized for critical sections" is not a logically consistent statement. As "critical section" is actually the synchronization primitive itself, in real code it may mean a) you are using critical section correctly and the threads are actually synchronized; b) you are not using critical section; or don't use it properly, which may lead to any unpredictable behavior, including deadlock. Actually the term "critical section" may mean two different related things: 1) critical section object implementing mutual exclusion; 2) some section of code interlocked using this primitive.

You just need to understand how mutual exclusion works:
http://en.wikipedia.org/wiki/Mutual_exclusion[^],
http://en.wikipedia.org/wiki/Deadlock[^].

Note that mutual exclusion is not the method of eliminating of a dead lock, not at all. This is a method of keeping integrity of some code fragment. Imagine you have two integers A and B, accessible by two or more threads. Imaging that you have an invariant: A + B == C, where C is some constant. Then you need to provide some atomic operation to keep this invariant in a multhreaded situation. (See also: http://en.wikipedia.org/wiki/Atomicity_%28programming%29[^]). How can you do it? For example
int delta = //...
A += delta;
B -= delta;



想象一下,如果一个线程增加其中一个值,然后其他一些线程在第一个线程完成之前执行相同操作会发生什么,并使用结果 A B ?你的不变性会怎样?为了防止这种情况,只允许一次只有一个线程进入这段代码。这是通过互斥锁或关键部分对象(实际上是互斥锁的轻型版本)完成的。请注意,使用任何线程API,只需使用ifs和其他编程控制结构,绝对不可能实现此效果。互斥锁将线程放入队列并处于特殊等待状态,其中线程被切换出来并且没有使用任何CPU时间,直到它被唤醒(通过超时,来自互斥锁的绿灯,其他事件,涉及中断系统) )。



这是众所周知的,经典的截止日期方式:


Imagine what happens if one thread increments one of these values, and then some other thread does the same, before first thread is done, and uses resulting A and B? What happens to your invariance? To prevent that, it's enough to allow only one thread at a time entering this fragment of code. This is done via mutexes or critical section objects (light version of mutex, actually). Note that it is absolutely impossible to achieve this effect but using any threading APIs, just with "ifs" and other programming control structures. A mutex put threads in a queue and in the special wait state where a thread is switched out and is not using any CPU time, until its waken up (by timeout, "green light" from a mutex, other events, interrupt system is involved).

And this is the well-known, classical way of making a deadline:

LPCRITICAL_SECTION sc1 = //...
LPCRITICAL_SECTION cs2 = //...

// this is how nesting works:
EnterCriticalSection(sc1);
EnterCriticalSection(sc2);
//some code...
LeaveCriticalSection(sc2);
LeaveCriticalSection(sc1);
// can you understand why could you possibly face nesting at all?

// expect deadlock:
EnterCriticalSection(sc1);
EnterCriticalSection(sc2);
LeaveCriticalSection(sc1);
LeaveCriticalSection(sc2);





我会留下你的家庭运动来弄清楚为什么这是一个僵局。这是一项非常有用的练习;它会帮助你理解它是如何工作的。







理解这一点很重要使用调试器甚至测试无法检测到典型的线程错误,因为操作的行为和顺序开始依赖于时序。代码应该在理论上进行分析和纠正。



[结束编辑]



请参阅:

http:// msdn。 microsoft.com/en-us/library/windows/desktop/ms682608%28v=vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/ desktop / ms684169%28v = vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686360%28v=vs.85%29。 aspx [ ^ ]。



另见我过去的回答:编写线程安全代码 [ ^ ]。



-SA



I'll leave for you home exercise to figure out why this is a deadlock. This is a very useful exercise; it will help you to understand how it all works.



It's important to understand that typical threading bugs cannot be detected using the debugger and even testing because the behavior and order of operations start to depend on timing. The code should be analyzed and corrected theoretically.

[END EDIT]

Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684169%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686360%28v=vs.85%29.aspx[^].

See also my past answer: Write thread safe code[^].

—SA


这篇关于线程未同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆