ENH: Do better test for pthreads

This commit is contained in:
Andy Cedilnik 2003-08-01 16:48:41 -04:00
parent cb4e99f712
commit ff5f0312de
2 changed files with 69 additions and 14 deletions

View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* runner(void*);
int res = 0;
int main()
{
pthread_t tid[2];
pthread_create(&tid[0], 0, runner, (void*)1);
pthread_create(&tid[1], 0, runner, (void*)2);
usleep(1); // for strange behavior on single-processor sun
pthread_join(tid[0], 0);
pthread_join(tid[1], 0);
return res;
}
void* runner(void* args)
{
int cc;
for ( cc = 0; cc < 10; cc ++ )
{
printf("%d CC: %d\n", (int)args, cc);
}
res ++;
return 0;
}

View File

@ -16,6 +16,30 @@ ENDIF(CMAKE_SYSTEM MATCHES IRIX)
CHECK_INCLUDE_FILE("pthread.h" CMAKE_HAVE_PTHREAD_H)
IF(CMAKE_HAVE_PTHREAD_H)
IF(NOT CMAKE_HAVE_SPROC_H)
IF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
MESSAGE(STATUS "Check if compiler accepts -pthread")
TRY_RUN(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
${CMAKE_BINARY_DIR}
${CMAKE_ROOT}/Modules/CheckForPthreads.c
CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
OUTPUT_VARIABLE OUTPUT)
IF(THREADS_HAVE_PTHREAD_ARG)
IF(THREADS_PTHREAD_ARG MATCHES "^2$")
MESSAGE(STATUS "Check if compiler accepts -pthread - yes")
ELSE(THREADS_PTHREAD_ARG MATCHES "^2$")
MESSAGE(STATUS "Check if compiler accepts -pthread - no")
FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeError.log
"Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
ENDIF(THREADS_PTHREAD_ARG MATCHES "^2$")
ELSE(THREADS_HAVE_PTHREAD_ARG)
MESSAGE(STATUS "Check if compiler accepts -pthread - no")
FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeError.log
"Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
ENDIF(THREADS_HAVE_PTHREAD_ARG)
ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
IF(THREADS_HAVE_PTHREAD_ARG)
SET(CMAKE_THREAD_LIBS_INIT "-pthread")
ELSE(THREADS_HAVE_PTHREAD_ARG)
CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
IF(CMAKE_HAVE_PTHREADS_CREATE)
SET(CMAKE_THREAD_LIBS_INIT "-lpthreads")
@ -30,6 +54,7 @@ IF(CMAKE_HAVE_PTHREAD_H)
SET(CMAKE_THREAD_LIBS_INIT "-lthread")
ENDIF(CMAKE_HAVE_THR_CREATE)
ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*")
ENDIF(THREADS_HAVE_PTHREAD_ARG)
ENDIF(NOT CMAKE_HAVE_SPROC_H)
ENDIF(CMAKE_HAVE_PTHREAD_H)