pthread_cancel(3) - wariua/manpages-ko GitHub Wiki
pthread_cancel - μ€λ λμκ² μ·¨μ μμ² λ³΄λ΄κΈ°
#include <pthread.h>
int pthread_cancel(pthread_t thread);
-pthread
λ‘ λ§ν¬.
pthread_cancel()
ν¨μλ μ€λ λ thread
μκ² μ·¨μ μμ²μ 보λΈλ€. λμ μ€λ λκ° μ·¨μ μμ²μ λ°μν μ§, κ·Έλ λ€λ©΄ μΈμ μΌμ§λ κ·Έ μ€λ λμ ν΅μ νμ μλ λ κ°μ§ μμ±, μ¦ μ·¨μ κ°λ₯μ± μνμ μ νμ λ¬λ € μλ€.
μ€λ λμ μ·¨μ κ°λ₯μ± μνλ pthread_setcancelstate(3)λ‘ κ²°μ νλ©° νμ±(μ μ€λ λμ κΈ°λ³Έκ°)μ΄κ±°λ λΉνμ±μΌ μ μλ€. μ€λ λμμ μ·¨μλ₯Ό λΉνμ±ννμΌλ©΄ λ€μ μ·¨μλ₯Ό νμ±νν λκΉμ§ μ·¨μ μμ²μ΄ νμ λ¨μ μλλ€. μ€λ λμμ μ·¨μλ₯Ό νμ±ννμΌλ©΄ μ·¨μ κ°λ₯μ± μ νμ΄ μ·¨μ λ°μ μμ μ κ²°μ νλ€.
μ€λ λμ μ·¨μ μ νμ pthread_setcanceltype(3)μΌλ‘ κ²°μ νλ©° λΉλκΈ°λ μ°κΈ°(μ μ€λ λμ κΈ°λ³Έκ°)μΌ μ μλ€. λΉλκΈ° μ·¨μλ μ€λ λκ° μΈμ λ (μΌλ°μ μΌλ‘ μ¦μμ΄μ§λ§ μμ€ν μμ 보μ₯νμ§ μμ) μ·¨μλ μ μλ€λ λ»μ΄λ€. μ°κΈ° μ·¨μλ μ€λ λμμ μ·¨μμ μΈ ν¨μλ₯Ό νΈμΆν λκΉμ§ μ·¨μκ° μ°κΈ°λλ€λ λ»μ΄λ€. μ·¨μμ μ΄κ±°λ μ·¨μμ μΌ μλ μλ ν¨μλ€μ λͺ©λ‘μ΄ pthreads(7)μ μλ€.
μ·¨μ μμ²μ λ°μν λ thread
μμλ λ€μ λ¨κ³λ€μ΄ (μ°¨λ‘λ‘) μ΄λ€μ§λ€.
-
μ·¨μ μ 리 νΈλ€λ¬λ₯Ό (μ§μ΄λ£μ μμ λ°λλ‘) κΊΌλ΄μ νΈμΆνλ€. (pthread_cleanup_push(3)) μ°Έκ³ .)
-
μ€λ λλ³ λ°μ΄ν° μλ©Έμλ€μ΄ λͺ μΈ μ λ μμλ‘ νΈμΆλλ€. (pthread_key_create(3) μ°Έκ³ .)
-
μ€λ λκ° μ’ λ£λλ€. (pthread_exit(3) μ°Έκ³ .)
μ λ¨κ³λ€μ pthread_cancel()
νΈμΆκ³Ό λΉλκΈ°μ μΌλ‘ μ΄λ€μ§λ€. pthread_cancel()
μ λ°ν μνλ μ·¨μ μμ²μ΄ μ±κ³΅μ μΌλ‘ νμ λ€μ΄κ°λμ§λ₯Ό νΈμΆμμκ² μλ € μ€ λΏμ΄λ€.
μ·¨μλ μ€λ λκ° μ’
λ£ν νμ pthread_join(3)μΌλ‘ κ·Έ μ€λ λμ ν©λ₯νλ©΄ μ€λ λ μ’
λ£ μνλ‘ PTHREAD_CANCELED
λ₯Ό μ»λλ€. (μ€λ λμ ν©λ₯νλ κ²μ μ·¨μκ° μλ£λλμ§ μ μ μλ μ μΌν λ°©λ²μ΄λ€.)
μ±κ³΅ μ pthread_cancel()
μ 0μ λ°ννλ€. μ€λ₯ μ 0 μλ μ€λ₯ λ²νΈλ₯Ό λ°ννλ€.
ESRCH
- IDκ°
thread
μΈ μ€λ λλ₯Ό μ°Ύμ μ μλ€.
μ΄ μ μμ μ¬μ©νλ μ©μ΄λ€μ λν μ€λͺ μ attributes(7)λ₯Ό 보λΌ.
μΈν°νμ΄μ€ | μμ± | κ° |
---|---|---|
pthread_cancel() |
μ€λ λ μμ μ± | MT-Safe |
POSIX.1-2001, POSIX.1-2008.
리λ
μ€μμλ μκ·Έλμ μ΄μ©ν΄ μ·¨μλ₯Ό ꡬννλ€. NPTL μ€λ λ© κ΅¬νμμλ 첫 λ²μ§Έ μ€μκ° μκ·Έλ(μ¦ μκ·Έλ 32)μ μ΄ μ©λμ μ΄λ€. LinuxThreadsμμλ μ€μκ° μκ·Έλμ΄ μ¬μ© κ°λ₯νλ©΄ λ λ²μ§Έ μ€μκ° μκ·Έλμ μ°κ³ μλλ©΄ SIGUSR2
λ₯Ό μ΄λ€.
μλ νλ‘κ·Έλ¨μμλ μ€λ λλ₯Ό μμ±νλ€κ° μ·¨μνλ€. λ©μΈ μ€λ λκ° μ·¨μλ μ€λ λμ ν©λ₯ν΄μ μ’
λ£ μνκ° PTHREAD_CANCELED
μΈμ§ νμΈνλ€. λ€μ μ
Έ μΈμ
μ νλ‘κ·Έλ¨ μ€ν μ μ΄λ»κ² λλμ§ λ³΄μ¬ μ€λ€.
$ ./a.out
thread_func(): started; cancellation disabled
main(): sending cancellation request
thread_func(): about to enable cancellation
main(): thread was canceled
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
static void *
thread_func(void *ignored_argument)
{
int s;
/* μ·¨μλ₯Ό μ μ λΉνμ±νν΄μ μ·¨μ μμ²μ
μ¦μ λ°μνμ§ μλλ‘ νκΈ° */
s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
printf("thread_func(): started; cancellation disabled\n");
sleep(5);
printf("thread_func(): about to enable cancellation\n");
s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
/* sleep()μ μ·¨μμ μ */
sleep(1000); /* μ λ€μ΄ μλ λμ μ·¨μλ¨ */
/* μ λ μ¬κΈ°λ‘ μ μ΄ */
printf("thread_func(): not canceled!\n");
return NULL;
}
int
main(void)
{
pthread_t thr;
void *res;
int s;
/* μ€λ λ μμνκ³ μ·¨μ μμ²μ 보λ΄κΈ° */
s = pthread_create(&thr, NULL, &thread_func, NULL);
if (s != 0)
handle_error_en(s, "pthread_create");
sleep(2); /* μ€λ λμκ² μμν μκ° μ£ΌκΈ° */
printf("main(): sending cancellation request\n");
s = pthread_cancel(thr);
if (s != 0)
handle_error_en(s, "pthread_cancel");
/* μ€λ λμ ν©λ₯ν΄μ μ’
λ£ μν 보기 */
s = pthread_join(thr, &res);
if (s != 0)
handle_error_en(s, "pthread_join");
if (res == PTHREAD_CANCELED)
printf("main(): thread was canceled\n");
else
printf("main(): thread wasn't canceled (shouldn't happen!)\n");
exit(EXIT_SUCCESS);
}
pthread_cleanup_push(3), pthread_create(3), pthread_exit(3), pthread_join(3), pthread_key_create(3), pthread_setcancelstate(3), pthread_setcanceltype(3), pthread_testcancel(3), pthreads(7)
2019-03-06