Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #88234

Re: Greatly simplify multithreaded access to global object

From "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Greatly simplify multithreaded access to global object
Date 2022-12-22 15:39 -0800
Organization A noiseless patient Spider
Message-ID <to2prh$1ga13$1@dont-email.me> (permalink)
References <b5d4afb7-1e6d-4753-bbde-963f9331720bn@googlegroups.com> <PR2pL.4177$%os8.1602@fx03.iad>

Show all headers | View raw


On 12/22/2022 12:11 PM, Scott Lurndal wrote:
> Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> writes:
>>
>> I don't know if I've re-invented the wheel here but I can't remember having seen something like this in the C++ standard library nor Boost nor wxWidgets.
>>
>> Let's say we have a multi-threaded program, it has a main GUI thread and five worker threads, giving a total of six threads.
>>
>> The program at all times has a status string, which is a global 'std::string' object. All six threads read and write the global status string.
>>
> 
> Rather than contending for single string updates, which can never scale,
> keep a string private to each thread where each thread updates the
> string as required without locking;  when you need the full status
> append the six strings into a single string.   

Yes. This is analogous to split counters. Each thread keeps its own 
count. If you want to know the sum, well, sum up all of the threads 
counters. It scales fairly decently. A lot better than a single shared 
counter! :^)



> All you need to synchronize
> is fetching the string from each thread and if you do it right, you'll
> likely not even need to synchronize access from the main thread to
> the strings (e.g. each thread has a pointer to the current status string
> and when the worker thread updates the status, it uses an atomic exchange to replace
> the pointer to the former status string with a pointer to the new
> status - when the main thread fetches the status it will get either
> the old or the new depending on how it races with the update).  The
> main thread can append the individual strings (or use/display each
> status independently).
> 
> The recursive mutex solution seems fragile and not scalable to large
> numbers of worker threads.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Greatly simplify multithreaded access to global object Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-22 11:41 -0800
  Re: Greatly simplify multithreaded access to global object Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-22 11:51 -0800
  Re: Greatly simplify multithreaded access to global object scott@slp53.sl.home (Scott Lurndal) - 2022-12-22 20:11 +0000
    Re: Greatly simplify multithreaded access to global object Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-22 12:25 -0800
    Re: Greatly simplify multithreaded access to global object "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-22 15:39 -0800
    Re: Greatly simplify multithreaded access to global object "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-22 15:42 -0800
  Re: Greatly simplify multithreaded access to global object David Brown <david.brown@hesbynett.no> - 2022-12-22 21:41 +0100
    Re: Greatly simplify multithreaded access to global object Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-22 23:05 +0200
      Re: Greatly simplify multithreaded access to global object David Brown <david.brown@hesbynett.no> - 2022-12-23 00:11 +0100
  Re: Greatly simplify multithreaded access to global object Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-22 23:01 +0200
  Re: Greatly simplify multithreaded access to global object Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-31 07:41 -0800
    Re: Greatly simplify multithreaded access to global object Muttley@dastardlyhq.com - 2022-12-31 16:02 +0000
      Re: Greatly simplify multithreaded access to global object David Brown <david.brown@hesbynett.no> - 2022-12-31 18:45 +0100

csiph-web