Subject: Re: On multithreaded cclient

From: "Alex Shvedov" <[email-masked]>
Date: Thu, 10 Dec 1998 11:04:05 -0500

>Alex Shvedov writes:
>> Some time ago I posted a question whether anyone has a multithreaded
version
>> of cclient; nobody responded.
>>
>> Now I know what changes are necessary to prepare such version, though I
>> don't have it ready yet (to be exact, I have it, but a crude one, with
>> locks/unlocks, which is bad).
>>
>> Is anyone interested? Or I'm the only one?
>>

>We discussed that on the c-client list some time ago and ended up with
>Mark Crispin reassuring us that c-client itself is thread-safe (apart
>from few init functions).

>Thus there is no need for any patches - if he's right.

>He says that data is only modified on a per-stream basis so as long as
>you don't have multiple threads accessing the same mail stream,
>everything is fine.

>What exactly are your changes?

>I'm working on an email system which I will have to multi-thread soon.

>--
>Karsten Ballüder http://www.phy.hw.ac.uk/~karsten/ mailto:[email-masked]
>Physics Department, Heriot-Watt University, Edinburgh, EH14 4AS, Scotland
>                     Tel. +44-(0)-131-4513068  Fax. +44-(0)-131-4513136
>           "In a world without fences, who needs Gates?"

I'm afraid that Mark is wrong, at least, that's what my tests told me. There
are at least three problems still to be solved while multithreading cclient
(at least, this is true for versions 4.2 and 4.4 I tested).

It's a long story to describe them in detail here and now, but the test is
really simple: create two threads that talk to the same server (or to two
different servers) and try to open these two streams. If you'll manage to do
this, I'll eat my hat (lucky not to have one).

I did exactly this - and failed on attempt number 2. That's why I went into
mutexing (a dumb way, no doubt, but it shows where threads confuse each
other) and that's how I know that there are 3 groups of problems, not 2 or
4, yet to be solved.

Alex Shvedov.

---

From: Karsten Ballueder <[email-masked]>
Date: Thu, 10 Dec 1998 16:29:46 +0000 (GMT)

Unfortunately I cannot look at your patches myself any time soon, but
as I know that I need to use cclient in a multi-threaded environement, 
I'm really interested to learn more.

If you could explain those "3 groups of problems", i.e. where and how
the conflicts occur, that would be very helpful. If Mark is wrong and
there is a problem with a one-thread-per-stream setup than we should
probably get it fixed.

---

From: Mark Crispin <[email-masked]>
Date: Thu, 10 Dec 1998 12:35:48 -0800 (PST)

> this, I'll eat my hat (lucky not to have one).

As far as I know, the problem is that you need a C library with a thread-safe
version of strtok().  If your C library's strtok() isn't thread-safe, you need
to replace it with one that is.  strtok() is used throughout c-client,
particularly in the IMAP code.

*BEGIN FLAME*
Some C libraries have a thread-safe strtok() alternative that requires you to
keep track of which thread you are for the global used by strtok().  It's a
great example of people who don't understand the concept of good software
engineering; if the application can keep track of per-thread context, a
library routine can do so as well.  Since c-client is itself a library, it's
no more work for the C library to do it than c-client.  Since the problem is
in the C library, the C library should fix it rather than making every program
in the world do so.

It isn't as if these are new concepts either.  Perhaps if UNIX geeks paid more
attention to what other operating systems were doing 10-20 years ago instead
of trying to kill them all off, we wouldn't be reinventing all this stuff
today.
*END FLAME*

Even with this problem, c-client is definitely thread-safe as long as a thread
can not be scheduled while another thread is inside c-client.  I have an
application that does this.