Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...
Posted
Comments
Sergey Alexandrovich Kryukov 8-Sep-11 0:36am    
Is it C++ or C++/CLI (in C++ there is not syntactic element called "lock"); what lock do you mean, more exactly? They are all conceptually similar, so this if more for the sake of convenience of explanation.
---SA
Sergey Alexandrovich Kryukov 8-Sep-11 0:38am    
Also, indicate your platform.
--SA

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:

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


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
 
Share this answer
 
v2
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 :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900