FLTK logo

Re: [fltk.coredev] RFC: Fl::enable_locks()

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.coredev  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: RFC: Fl::enable_locks() Michael Sweet Apr 18, 2021  
 
Albrecht,

On Apr 17, 2021, at 1:00 PM, Albrecht Schlosser <AlbrechtS.fltk@online.de> wrote:

On 4/17/21 2:25 PM Michael Sweet wrote:

Another crazy option: just have Fl::lock() be a no-op if the run loop is not active and then have Fl::run() call Fl::lock() unconditionally. When threading is enabled (which should be all the time now) the right thing happens without any special initialization, and Fl::lock()/unlock() can continue to be used for synchronization as before.

Mike, IIRC there's still the "side effect" that the first call into Fl::lock() enables the locking and unlocking feature. Hence, if you never call it, locking and unlocking will not be done by the FLTK thread (event loop), which is probably one of "fast and light" design aspects of FLTK.

Calling lock/unlock methods in the event loop, even for single-threaded applications, isn't going to add any detectable change in performance.  Consider the following simple benchmark program that locks and unlocks a mutex one billion times:

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>


int main(void)
{
  struct timeval start, end;
  int i;
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  double secs;


  gettimeofday(&start, NULL);

  for (i = 0; i < 1000000000; i ++)
  {
    pthread_mutex_lock(&mutex);
    pthread_mutex_unlock(&mutex);
  }

  gettimeofday(&end, NULL);

  secs = end.tv_sec - start.tv_sec + 0.000001 * (end.tv_usec - start.tv_usec);

  printf("%.3f seconds\n", secs);

  return (0);
}

On my new M1 MacBook Pro, it took just 8.659 seconds to complete, which means that each pair of lock/unlock took 8.659 *picoseconds*.  On my older 18-core iMac Pro it took a little over twice the time - 18.036 seconds - which works out to 18 picoseconds per pair.  On a Raspberry Pi 0 (probably the slowest of my computing devices) it took 155.639 seconds or about 155 picoseconds per lock/unlock pair.

So honestly, the overhead of using threading locks, even for a single-threaded program, is so small that I would recommend that we make FLTK thread-safe by default.

________________________
Michael Sweet



--
You received this message because you are subscribed to the Google Groups "fltk.coredev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkcoredev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkcoredev/6B93C6F7-2171-432D-90C8-CA4DB0449AA1%40gmail.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.