one processor with multiple core and threads

T

Tony Johansson

Hi!

If you have a computer with one processor and multiple core and you lock a
section is it then possible
that two threads can lock this section simultaneously.
I mean when you have one processor with multiple core you can have two
threads executing simultaneously so
this would be possible.
What would happen you can't have two threads own the lock to a section of
code at the same time.

//Tony
 
R

Random

If you have a computer with one processor and multiple core and you lock a
section is it then possible
that two threads can lock this section simultaneously.

No. Thats the point of a critical section.
 
A

Arne Vajhøj

If you have a computer with one processor and multiple core and you lock a
section is it then possible
that two threads can lock this section simultaneously.
I mean when you have one processor with multiple core you can have two
threads executing simultaneously so
this would be possible.
What would happen you can't have two threads own the lock to a section of
code at the same time.

With a lock on the same object then the two threads will not
execute the section code protected at the same time no matter
how many cores.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
With a lock on the same object then the two threads will not
execute the section code protected at the same time no matter
how many cores.

Arne

I mean if you have this statement somewhere in the code
lock(locker)
{
.. . .
}

Then if I use one processor with multiple core is then possible that two
threads executes the statement lock(locker)
simultaneously so two threads has a lock on section of code ?

//Tony
 
R

Rick Lones

Tony said:
I mean if you have this statement somewhere in the code
lock(locker)
{
. . .
}

Then if I use one processor with multiple core is then possible that two
threads executes the statement lock(locker)
simultaneously so two threads has a lock on section of code ?

//Tony

No, only one core will be granted the lock. There are hardware capabilities
which support software locking. The .Net runtime will be using those.

-rick-
 
A

Arne Vajhøj

I mean if you have this statement somewhere in the code
lock(locker)
{
. . .
}

Then if I use one processor with multiple core is then possible that two
threads executes the statement lock(locker)
simultaneously so two threads has a lock on section of code ?

lock(locker) may be reached by two threads, but the code inside
will not be executed in parallel, because one of the threads
will block in the lock statement until the other thread exits
the code section.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top