我如何将锁分配给当前线程 [英] How i can assign lock to current thread

查看:62
本文介绍了我如何将锁分配给当前线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在线程中.我想将当前线程分配给锁.

在通过当前线程获取锁之前,我想使以前空闲的锁繁忙..

请帮助..........我想要代码...

In threading. I want to assign to the lock the current thread.
and
Before acquire the lock by the current thread, I want to make the lock busy that was previously free..

Please help............. I want the code...

推荐答案

没有锁分配之类的概念.锁不能忙或闲.这些概念与您可能想到的完全不同.

在C ++/CLI(.NET)中,它可以是锁或System.Threading.ReaderWriterLockSlim;在本机C ++中,它可以是 CriticalSection Mutex (请参见http://www.curly-brace.com/lock.html [ Edsger Wybe Dijkstra ( http://en. wikipedia.org/wiki/Edsger_W._Dijkstra [ ^ ]),是荷兰语"wait"和"release"的缩写.

因此,在伪代码中,它看起来像这样:

There is no such concept as lock assignment. A lock cannot be busy or free. The concepts are very different from what you might think of.

In C++/CLI (.NET) it can be lock or System.Threading.ReaderWriterLockSlim, in native C++ it can be CriticalSection or Mutex (see http://www.curly-brace.com/lock.html[^], for example).

The right programming practice is only one — sandwiching some access to a shared resource between acquiring (P) and releasing (V) of some thread synchronization object of the type mentioned above, a lock; "P" and "V" are original terms by Edsger Wybe Dijkstra (http://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]), abbreviations from Dutch "wait" and "release".

So, in pseudo-code, it looks like this:

method AccessSharedResource() {
    Lock.P();
    try {
       WorkWithSharedResource();
    } finally {
       Lock.V();
    } end exception
} end method AccessSharedResource



函数AccessSharedResource一次由多个线程运行;所有线程还使用锁Lock的相同实例;该构造的目标是允许所有线程调用WorkWithSharedResource,但一次只能调用一个线程.同样,所有必须等待轮换的线程都应消耗零的CPU时间.

所有这些原语都在线程上工作.一些同步线程属于同一进程,另一些同步线程则可以对属于不同进程的线程执行相同的操作(命名为Mutex可以做到这一点;之所以这样命名,是因为需要系统范围内的唯一标识来标识该线程;因为进程是隔离的,并且所有引用/指针/句柄仅对它们的进程有意义-地址空间是隔离的;地址的相同数值表示在不同进程中的不同地址).无论如何,在所有情况下,这些同步原语仅影响线程.

P的调用是阻止.如果只有一个线程,则此函数将立即返回,但此线程已获取一个锁.下一个线程在此调用时被阻塞:OS将其关闭,并保持在特殊的等待状态,不浪费CPU时间.实际上,在唤醒其中一个线程之前,OS永远不会将等待线程调度回执行.它可以通过特殊方法唤醒,例如中止(.NET; Windows中也有类似的技术与异常播种方法相关,这需要花费大量篇幅来说明其工作原理),超时到期,以及-主要是-由另一个线程执行的对V 的调用.实际上,在此调用中,OS立即查看队列,该队列中等待相同锁实例的线程被累积起来,选择队列中的第一个线程,将其返回到工作状态并安排执行时间.新线程进入PV之间的区域,在这种情况下,调用WorkWithSharedResource.其余的等待线程将保持等待状态,直到唤醒下一个线程为止.

这样,OS保证所有线程都能通过,尝试使吞吐量最大化,并保证一次只能有一个线程可以访问共享资源.

—SA



A function AccessSharedResource is run by more than one thread at a time; all threads also use the same instance of the lock Lock; and the goal of this construct is to allow all threads to call WorkWithSharedResource, but only one thread at a time. Also, all the thread which have to wait for their turn should consume zero CPU time.

All those primitives work on threads. Some synchronize threads of the same process, some other can do the same to the threads belonging to different processes (named Mutex can do that; it is named because system-wide unique identification is needed to identify the thread; as processes are isolated and all references/pointers/handles only make sense withing their processes — address spaces are isolated; same numeric value of the address means different address in different processes). Anyway, in all cases, those synchronization primitives affect only the threads.

A call to P is blocking. If there is only one thread, this function returns immediately, but a lock is acquired by this thread. The next threads a blocked at this call: OS switches them off and keep at the special wait state wasting no CPU time. Practically, OS never schedule waiting threads back to execution until one of the threads is awaken. It can be awaken by special means like abort (.NET; also there is a similar technique in Windows related to exception seeding method — it would take a whole big article to explain how it works), timeout expiration, and — main thing — by a call to V performed by another thread. Practically, on this call, OS immediately looks at the queue where the threads waiting on the same instance of lock are accumulated, picks the first in queue, returns it to a working state and schedules for execution. New thread enters the area between P and V, in this case, calls WorkWithSharedResource. Remaining waiting threads remain in wait state until next one is waken up.

In this way, OS guarantees that all threads pass, tries to maximize throughput and guarantees that only one thread at a time can access the shared resource.

—SA


首先,您不能为线程分配锁(除非内核进行了调整),线程本身可以获取锁.

当某个锁已经被另一个线程获取时,锁只会处于忙状态(busy不是正确的字).为了模拟这种情况,您需要创建两个线程,获取我的一个线程的锁,然后休眠该线程,当尝试通过另一个线程获取锁时,您会发现忙锁.

好吧,您应该改善问题并提及您的操作系统(Win,Linux等)以获得更准确的答案.

祝你好运:-)
First of all you can''t assign a lock to thread (unless kernel tweaks), a thread itself can acquire lock.

And a lock is only busy (busy is not right word) when it is already acquired by some another thread. To simulate this case you need to create two threads, acquire lock my one and sleep that thread and when to try to acquire lock by another thread then you will found busy lock.

Well you should improve your question and mention your OS (Win, Linux etc) for more accurate answer.

Good Luck :-)


这篇关于我如何将锁分配给当前线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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