What does it mean for CMake to find a “pthread-compatible” library?

  Kiến thức lập trình

CMake’s FindThreads script documentation says that the variable CMAKE_USE_PTHREADS_INIT will be set “if the found thread library is pthread compatible.”

What does that mean? If it’s an actual POSIX threads implementation, then obviously it’s “pthread compatible”; but what are other “compatible” threads libraries? And what assumptions can I make about them? To take the most basic example: Can I safely include pthread.h?

tl;dr: “pthread-compatible” means you can assume it’s good old pthreads.

(Looking at the FindThreads.cmake source (as of CMake 3.28.3), the answer seems to be:)

This flag will be set if any of the following two conditions hold:

  1. Your system has library called pthread or pthreads you can link against; or
  2. The following C program build successfully:
    #include <pthread.h>
    
    static void* test_func(void* data)
    {
      return data;
    }
    
    int main(void)
    {
      pthread_t thread;
      pthread_create(&thread, NULL, test_func, NULL);
      pthread_detach(thread);
      pthread_cancel(thread);
      pthread_join(thread, NULL);
      pthread_atfork(NULL, NULL, NULL);
      pthread_exit(NULL);
    
      return 0;
    }
    

In the second case you know exactly what’s going on, so no problems there. The first case is a bit of a leap of faith, since you haven’t checked exactly what that library is – but then, so are many other library tests; you usually accept that a library calling itself foo is indeed the libfoo you are looking for.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT