Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

good reference on threads

90 views
Skip to first unread message

Lynn McGuire

unread,
Jun 10, 2021, 5:59:52 PM6/10/21
to
Is there a good reference on threads ? I would like to delegate the
writing of our binary file to a background thread if possible. Our
Win32 Windows desktop program is mostly MFC with about 450,000 lines of
C++ code.

I do suspect that this is not doable, due to the nature of the data
being used for dialogs and such.

Thanks,
Lynn

Öö Tiib

unread,
Jun 10, 2021, 6:49:31 PM6/10/21
to
Threads can be used in various ways. Making half million lines
code-base that is not thread safe to use threads is quite painful
trick to pull. "Not doable" is not what I've met, it is always "what it
will cost". I would maybe study Boost.Asio as it has nice clear
philosophy of how to use threads. Then the question will shift
from "how to make multiple threads to concurrently use same
data?" to "how to make seamless I/O between threads that all
have their own data?".

Öö Tiib

unread,
Jun 10, 2021, 8:59:07 PM6/10/21
to
Forgot to give link to brief introduction to it:
<https://theboostcpplibraries.com/boost.asio>
It is always viewed as some kind of networking, multiprocessing
and/or RPC tool in books. You can indeed do all that great with it.
For example you can use separate process for writing that binary
file with it. But you can also use just one thread, communicated
with through Boost.Asio without separate process. It is likely
one of safest ways of introducing multithreading into
single-threaded legacy code.

Lynn McGuire

unread,
Jun 10, 2021, 9:32:04 PM6/10/21
to
I already have threads that have their own data. I guess that I should
have mentioned that.

I want multiple threads to share the massive dataset that we use. Very
tricky, I have tried sharing data between the threads and it was a disaster.

Lynn

Lynn McGuire

unread,
Jun 10, 2021, 11:03:48 PM6/10/21
to
I found "C++ Concurrency in Action, 2nd Edition" by Williams, Anthony.
Has anyone read this and found it to be a good education on threads ?

https://www.amazon.com/C-Concurrency-Action-Anthony-Williams/dp/1617294691/

Thanks,
Lynn McGuire



Chris M. Thomasson

unread,
Jun 10, 2021, 11:09:05 PM6/10/21
to
I used to converse with Anthony way back. He knows his threads! :^)

Öö Tiib

unread,
Jun 10, 2021, 11:31:50 PM6/10/21
to
It is about philosophy what you do. I prefer not to share data.

Several threads taking their turns to do something with data (X) is
obviously equivalent to just one thread (XMaster) accessing X and
rest of the threads telling to XMaster what they want from X in
asynchronous manner (for that I suggest asio).

It is easier that way to solve when too lot of work is needed to be
done with X so work queue of XMaster keeps growing.
XMaster can then split X into parts and delegate managing
of those parts to its own workers. Once all computing power is
occupied and queue full then it is time to buy more hardware.

And now if you used asio then it is all easy as asio is meant
for networking and RPC and your software has grown into
networking and RPC thanks to more hardware you bought.

Chris M. Thomasson

unread,
Jun 11, 2021, 12:32:41 AM6/11/21
to
On 6/10/2021 8:31 PM, Öö Tiib wrote:
> On Friday, 11 June 2021 at 04:32:04 UTC+3, Lynn McGuire wrote:
>> On 6/10/2021 5:49 PM, Öö Tiib wrote:
>>> On Friday, 11 June 2021 at 00:59:52 UTC+3, Lynn McGuire wrote:
[...]
> It is about philosophy what you do. I prefer not to share data.


Only share data when you have to! That is a decent mantra to learn.

[...]

Chris M. Thomasson

unread,
Jun 11, 2021, 12:34:03 AM6/11/21
to

Ian Collins

unread,
Jun 11, 2021, 4:18:04 AM6/11/21
to
Well it very much depends on whether you have multiple writers. If you
have a more readers than writers, std::shared_mutex is worth a look.

--
Ian.

Paavo Helde

unread,
Jun 11, 2021, 7:04:02 AM6/11/21
to
11.06.2021 04:31 Lynn McGuire kirjutas:
>
> I want multiple threads to share the massive dataset that we use.  Very
> tricky, I have tried sharing data between the threads and it was a
> disaster.

MFC is not thread-safe, so only the main thread can call any MFC functions.

For data exchange the most robust way is to use a couple of message
queues for passing events back and forth between the threads. Any data
which is not MT-safe to access should be copied by value into a new
non-shared object when putting into the queue.

One can easily build such a queue with a std::queue and a std::mutex.

For example, if a background queue wants to display a message to the
user, it can send an event to the main thread queue containing the
message. The main thread would check in a MFC OnIdle() function if there
are any events in the queue, and process them in the main thread.

Paavo Helde

unread,
Jun 11, 2021, 8:19:57 AM6/11/21
to
11.06.2021 14:03 Paavo Helde kirjutas:
> 11.06.2021 04:31 Lynn McGuire kirjutas:
>>
>> I want multiple threads to share the massive dataset that we use.
>> Very tricky, I have tried sharing data between the threads and it was
>> a disaster.
>
> MFC is not thread-safe, so only the main thread can call any MFC functions.
>
> For data exchange the most robust way is to use a couple of message
> queues for passing events back and forth between the threads. Any data
> which is not MT-safe to access should be copied by value into a new
> non-shared object when putting into the queue.
>
> One can easily build such a queue with a std::queue and a std::mutex.

s/std::queue/std::deque/

Lynn McGuire

unread,
Jun 11, 2021, 3:13:40 PM6/11/21
to
The problem is specifically too many writes slowing the user interface down.

Lynn


Chris M. Thomasson

unread,
Jun 12, 2021, 5:41:44 PM6/12/21
to
Oh I missed the MFC usage. Actually used it a couple of times in the
early to mid 90's. Hummm, well, for a multi_producer-to-single_consumer
relationship, if one can get over a LIFO order, this construction might
be of use to the OP:

https://groups.google.com/g/comp.lang.c++/c/1gppAtQpIQk/m/XX8-VDoMBgAJ

One can also reverse the LIFO to get a FIFO.

Cholo Lennon

unread,
Jun 12, 2021, 10:06:30 PM6/12/21
to
I have the 1st version of that book, it's a very good one on the
subject. AFAIK it is "the" book about C++ multi-threaded programming.

Regards

--
Cholo Lennon
Bs.As.
ARG


Keith Thompson

unread,
Jun 13, 2021, 12:47:47 AM6/13/21
to
Amazon doesn't have it in Kindle format, but the second edition (2019)
is available from the print, ebook, and audio formats.

I haven't read it, so I can't comment on how good it is.

https://www.manning.com/books/c-plus-plus-concurrency-in-action-second-edition

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Keith Thompson

unread,
Jun 13, 2021, 12:49:20 AM6/13/21
to
Keith Thompson <Keith.S.T...@gmail.com> writes:
> Cholo Lennon <cholo...@hotmail.com> writes:
>> On 6/11/21 12:03 AM, Lynn McGuire wrote:
>>> On 6/10/2021 4:59 PM, Lynn McGuire wrote:
>>>> Is there a good reference on threads ?  I would like to delegate
>>>> the writing of our binary file to a background thread if possible. 
>>>> Our Win32 Windows desktop program is mostly MFC with about 450,000
>>>> lines of C++ code.
>>>>
>>>> I do suspect that this is not doable, due to the nature of the data
>>>> being used for dialogs and such.
>>>>
>>>> Thanks,
>>>> Lynn
>>> I found "C++ Concurrency in Action, 2nd Edition" by Williams,
>>> Anthony. Has anyone read this and found it to be a good education on
>>> threads ?
>>> https://www.amazon.com/C-Concurrency-Action-Anthony-Williams/dp/1617294691/
>>
>> I have the 1st version of that book, it's a very good one on the
>> subject. AFAIK it is "the" book about C++ multi-threaded programming.
>
> Amazon doesn't have it in Kindle format, but the second edition (2019)
> is available from the print, ebook, and audio formats.

I meant "available from the publisher".

Lynn McGuire

unread,
Jul 1, 2021, 11:27:59 PM7/1/21
to
I bought the book. It looks intense. Not a very big font either.

Lynn

Chris M. Thomasson

unread,
Jul 2, 2021, 12:29:08 AM7/2/21
to
I have not seen his new book, but I am wondering if he mentions how to
use DWCAS in it with pure C++. I know he knows about it:

actually, read the whole thread where this message resides:

https://groups.google.com/g/lock-free/c/X3fuuXknQF0/m/Ho0H1iJgmrQJ

He calls it DCAS in that thread, buts DCAS and DWCAS are different
things. So, I am wondering if he mentions it in his 2nd edition.

Richard

unread,
Jul 26, 2021, 9:19:40 PM7/26/21
to
[Please do not mail me a copy of your followup]

Lynn McGuire <lynnmc...@gmail.com> spake the secret code
<s9ujq0$dhn$1...@dont-email.me> thusly:
I have read this and it's what I was going to recommend :)
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <http://terminals-wiki.org>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Richard

unread,
Jul 26, 2021, 9:22:08 PM7/26/21
to
[Please do not mail me a copy of your followup]

Lynn McGuire <lynnmc...@gmail.com> spake the secret code
<sbm13f$a1k$1...@dont-email.me> thusly:
It's organized in such a way that you can dig as deep (or as shallow)
as you like and still come away with a good understanding of what you
need to know. All my opinion, of course.

Anthony Williams is one of the primary authors of Boost.Threads, from
which we got std::thread. He also participates heavily in the ISO
standards process. It is *the* go to book for C++ multithreading and
concurrency.

Bonita Montero

unread,
Jul 26, 2021, 9:58:51 PM7/26/21
to
Google for: "c++ concurrency in action doctype:pdf"
And you find the PDF of the 1st ed.
0 new messages