48 lines · cpp
1// RUN: %clangxx -pthread %s -o %t2 3// RUN: %env_tool_opts=detect_invalid_join=true not %run %t 0 2>&1 | FileCheck %s4// RUN: %env_tool_opts=detect_invalid_join=true not %run %t 1 2>&1 | FileCheck %s5// RUN: %env_tool_opts=detect_invalid_join=true not %run %t 2 2>&1 | FileCheck %s6// RUN: %env_tool_opts=detect_invalid_join=true not %run %t 3 2>&1 | FileCheck %s --check-prefix=DETACH7 8// REQUIRES: glibc && (asan || hwasan || lsan)9 10#include <assert.h>11#include <ctime>12#include <errno.h>13#include <pthread.h>14#include <stdint.h>15#include <stdlib.h>16#include <unistd.h>17 18static void *fn(void *args) {19 sleep(1);20 return nullptr;21}22 23int main(int argc, char **argv) {24 int n = atoi(argv[1]);25 pthread_t thread;26 assert(!pthread_create(&thread, nullptr, fn, nullptr));27 void *res;28 if (n == 0) {29 while (pthread_tryjoin_np(thread, &res))30 sleep(1);31 pthread_tryjoin_np(thread, &res);32 } else if (n == 1) {33 timespec tm = {0, 1};34 while (pthread_timedjoin_np(thread, &res, &tm))35 sleep(1);36 pthread_timedjoin_np(thread, &res, &tm);37 } else if (n == 2) {38 assert(!pthread_join(thread, &res));39 pthread_join(thread, &res);40 } else if (n == 3) {41 assert(!pthread_detach(thread));42 pthread_join(thread, &res);43 }44 // CHECK: Joining already joined thread45 // DETACH: Joining detached thread46 return 0;47}48