Re: Understanding threading

From: valentin tihomirov (valentin_at_abelectron.com)
Date: 11/29/03


Date: Sat, 29 Nov 2003 16:55:43 +0200


> I'm currently writing a server which uses thread pooling; this is all
working fine at present but I've so far been overly
> careful with locking (currently using critical sections) and I believe
that this will have performance issues later on.
I don't know the difference of mutex vs. CirutSect. Mutexes should be
lighter; may be they are faster as well?

> I've heard a few people say that as long as no writing is performed on a
variable, then any amount of threads can read > it without any locking being
required. Is this correct?
This is the basic concpt of multithreading. However, synchonized access
still should be conserned, because you hardly have an object that need no
updates. There is even a special TMultiReadSingleWrite (or something, never
used myself) in Delphi.

> Also, I read a while back that TThread setting Terminated = True is an
atomic operation, yet at least in my version of > Delphi (rather dated
now -- v4.03) there doesn't appear to be any Interlockedxyz() API's used to
change it's value.
> So how can it be atomic?
May be this is true for all x86 platforms, basically CPUs tend to update
words as a whole, there is no write bit7 in byte 0x0001 instruction.
Furthermore, *Terminated* flag is a single-bit structure and it cannot be
inconsistent in principle.

> Currently I'm using Critical Sections to provide the locking system, but I
realise that they might not be the most
> efficient -- for a high-usage server what would be the best type of
synchronisation to use?
I think events+mutexes as (slightly) more efficient mechanism than messages,
cos messages has their own synch queues.
Multithreading improves performance because of concurrent execution with
expense of synchronization. Synchromnisation in inevitable and it is ALWAYS
consumes time and resources, even in hardware when you cross interclock
domains. Therefore, even advanced programmers avoid multithreading (use
overlapped I/O) if possible. BTW, it is overlapped I/O that is used in place
of multiple threads + blocking I/O in high-performance servers.



Relevant Pages

  • Re: Newbie: multithreading (?)
    ... >I am not sure if multithreading is the answer but i wonder what should i do ... >and how to prevent the "locking" of a form and event response when there is ... >What should i do to allow other user interaction and process the data in the ... Put a load of DoEvents in the bit that is doing the processing ...
    (microsoft.public.vb.general.discussion)
  • Re: CMultiLock example
    ... This is why naive approaches to multithreading and concurrency usually fail. ... In one famous example in the history of multithreading, a colleague of mine, Pete ... could get orders of magnitude performance improvement if you just ignored locking ... as OpenMP uses which also rely on hardware locking. ...
    (microsoft.public.vc.mfc)
  • Newbie: multithreading (?)
    ... I am not sure if multithreading is the answer but i wonder what should i do ... and how to prevent the "locking" of a form and event response when there is ... What should i do to allow other user interaction and process the data in the ...
    (microsoft.public.vb.general.discussion)
  • Re: A hang on code?
    ... but if you want to learn about multithreading in VB.NET, ... >There are some inbuilt method that waits for a procedure execution, ... And the administrator need to be able to open the server ... with no UI locking. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: [patch 3/3] mm: PageActive no testset
    ... >> being used for locking or without other synchronisation. ... Please read the FAQ at http://www.tux.org/lkml/ ... Prev by Date: ...
    (Linux-Kernel)