Skip to content

Conversation

@yannrichet
Copy link
Member

The f2c-translated C code had static local variables in multiple functions (setulb_, mainlb_, and 18+ other subroutines), making them non-thread-safe.

Static variables are shared across all function calls, causing race conditions, memory corruption, and segmentation faults in multi-threaded contexts.

This commit removes the static keyword from 202 local variable declarations, making each function call have its own independent local state on the stack. Only legitimate static uses (constant definitions and format strings) are preserved.

This enables true thread-safety without requiring mutex serialization.

Testing:

  • Library compiles successfully
  • All concurrent access tests pass
  • Object file analysis confirms variables are now stack-allocated

Related:

yannrichet and others added 3 commits October 17, 2025 11:18
The f2c-translated C code had static local variables in multiple functions
(setulb_, mainlb_, and 18+ other subroutines), making them non-thread-safe.
Static variables are shared across all function calls, causing race conditions,
memory corruption, and segmentation faults in multi-threaded contexts.

This commit removes the static keyword from 202 local variable declarations,
making each function call have its own independent local state on the stack.
Only legitimate static uses (constant definitions and format strings) are
preserved.

This enables true thread-safety without requiring mutex serialization.
yannrichet added a commit to libKriging/libKriging that referenced this pull request Dec 9, 2025
Use the fixed version of lbfgsb_cpp that removes static local variables
to enable true thread-safety. This eliminates the need for mutex
serialization in the optimizer.

Related PRs:
- yannrichet/lbfgsb_cpp#3
- libKriging/lbfgsb_cpp#6
Copy link
Member

@hpwxf hpwxf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J’y jette un œil complémentaire asap.

const int* iprint, char* csave,
bool* lsave, int* isave, double* dsave,
std::size_t len_task, std::size_t len_csave
ftnlen len_task, ftnlen len_csave
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dans quel cas as-tu besoin de changer le type associé à la taille ?

namespace lbfgsb {
extern "C" {
void setulb_(
int setulb_(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D’où vient le besoin du retour en int ?

"\002,/,\002 2 rounding errors dominate computat"
"ion.\002)";
static char fmt_9015[] = "(/,\002 Warning: more than 10 function and gr"
char fmt_9015[] = "(/,\002 Warning: more than 10 function and gr"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J’ai l’impression que beaucoup de variables statiques avec une telle valeur par défaut pourraient être passées en « static const » ou juste « const » (meilleur pour les perfs et la safety)

@yannrichet-asnr
Copy link

Désolé, c'est l'autre PR qu'il faut regarder :)
(et supprimer celle ci, mais je ne puex pas)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants