Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added mutex lock to NCCE

...

Code Block
bgColor#ffcccc
langc
mtx_t mutex;

void thread_foo(void *ptr) {
  uint32_t num;
  int result;
  int sock;

  /* sock is a connected TCP socket */
  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Error */
  }
  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), 0)) < 0) {
    /* Handle Error */
  }

  /* ... */

  if ((result = mtx_unlock(&mutex)) != thrd_success) {
    /* Handle Error */
  }
}

int main() {
  thrd_t thread;
  int result;

  if ((result = mtx_init(&mutex, mtx_plain)) != thrd_success) {
    /* Handle Error */
  }

  if (thrd_create(&thread,(void *)& thread_foo, NULL) != thrd_success) {
    /* Handle Error */
  }

  /* ... */

  thrd_join(thread, NULL);

  if ((result = mtx_destroy(&mutex)) != thrd_success) {
    /* Handle Error */
  }

  return 0;
}

...