News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Concurrecy and register values

Started by Danesh, June 21, 2005, 03:11:00 AM

Previous topic - Next topic

Danesh

Hi all,

I am writing a program which will create and control many threads. It now works fine, but I was thinking if some cases if the program be very busy so many threads start to work simutaneously. In that case, each thread may use registers so may it cause any conflict ? I mean in that case many threads may use same registers and values of registers may change, am I right or OS would make a copy of registers in each call and would take care of it by using Local and Global Descriptor Tables (LDT and GDT) ? I can also imagine when just one CPU is in use, so it would run at each time one thread so this problem would be solved by itself, but what about a system with two or more CPUs ? They may use registers at same time... am I right ?


chep

Each time the CPU switches tasks (ie. threads), the OS saves the previous thread's context (ie. CPU registers etc) and restores the next thread's context before allowing it to run.
As each CPU has it's own set of registers, your last question is irrelevant, no conflict is possible.


What you must take care of with multithreading is concurrent access to resources (ie. memory, files, ...) *if* one of the threads may be writing to the resource in question. If all threads are reading a resource, it doesn't need to be protected.

More simply put, multiple concurrent reads to a resource are OK most of the time, but writing must be exclusive (no other writes or reads at the same time).

See Critical Sections (in-process), Mutexes (inter-process), and the other Win32 synchronization objects, to know how to prevent multiple threads to access the same resource at a given time. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/synchronization.asp

Danesh

Well, Ok I am convienced now but about reading and writing to file or memory. While writing to file or memory is in progress no read operation can be done ? It would kill performance. However I had read somewhere that MySQL database has this weakness that during writing all other reading and writing processes will be suspended until writing ends but in latest version they had fixed this, I don't know how. It just strange that why two read and write processes can be executed at same time ?


roticv

That can happen if you have more than 1 processor.

chep

Quote from: Danesh on June 21, 2005, 12:17:05 PMIt just strange that why two read and write processes can be executed at same time ?
On single processor machines two operations can obviously not occur *at the same time* but they can be interleaved due to preemptive multitasking.
Eg:
- You have an unguarded 2048 bytes shared memory area, and 2 threads accessing it
- Thread 1 starts writing, and it manages to write 1024 bytes before the OS gives control to thread 2
- Thread 2 starts reading, but this one manages to read 2048 bytes...
So, the first 1024 bytes just read by thread 2 are the "new version" that thread 1 just wrote, while the last 1024 bytes read by thread 2 are the "old version" that was here before thread 1 started writing. So the buffer read by thread 2 is inconsistent, and this is Bad for you!

Quote from: Danesh on June 21, 2005, 12:17:05 PMWhile writing to file or memory is in progress no read operation can be done ? It would kill performance.
Well, I wouldn't say exactly this (maybe I misunderstand what you're saying?).
Each shared resource (be it a file, a memory area, ...) should be guarded by it's own synchronization object. This allows different resources to be accessed concurrently (by different threads), while each individual resource is guaranteed to be accessed by only one thread at a time. Thus the performance bottleneck is decreased.

I'm not sure this explanation is clear, but this is a vast and complicated subject and I don't really know how to correctly explain this in english. :red
Hope it helps anyway...

Danesh

roticv,

Thanks for your answer but I don't understand what you mean.

chep,

Thanks for your answer but protecting resources while they are shared is another story which should be managed by server or any program which shares resources between other users. The main question for me is if it possible (just technically and regardless of protecting shared resources issues) to read and write at the same time to disk or even memory, however I will test it practically.

Regards,

Danesh


chep

Quote from: Danesh on June 22, 2005, 12:05:46 AM
protecting resources while they are shared is another story which should be managed by server or any program which shares resources between other users.

I was really talking about resources shared by several threads *in the same process* (ie. multithreading synchronization issues).