Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed white space in examples, removed double definition of result. Also changed to comply with C11.

...

Code Block
bgColor#ffcccc
langc
volatile sig_atomic_t flag = 0;

void handler(int signum) {
  flag = 1;
}


voidint func(void *data) {  //* keep running until user sends SIGUSR1 */
  while (!flag) {
    /* ... */
  }
  return 0;
}


int main(void) {
  signal(SIGUSR1, handler); //* Undefined! */
  int result;
  thrd_t threadtid;
  
  int result;
  if ((result = thrd_create(&tid, func, NULL)) != thrd_success) {
    /* Handle Error */
  }

  /* ... */

  return 0;
}

...

Code Block
bgColor#ccccff
langc
atomic_flag flag = ATOMIC_VAR_INIT(0);


voidint func(void *data) {  //* keep running until user sends SIGUSR1 */
  while (!flag) {
    /* ... */
  }
  return 0;
}


int main(void) {
  int result;
  thrd_t threadtid;
  
  int result;
  if ((result = thrd_create(&tid, func, NULL)) != thrd_success) {
    /* Handle Error */
  }


  /* ... */


  /* Set flag when done */
  while (!atomic_flag_test_and_set( &flag)) { /* try again */ }


  return 0;
}

Exceptions

CON37:EX0: Platforms that provide defined behavior when multithreaded programs use custom signal handlers are exempt from this rule. This would include POSIX, for example.

...