::: 강좌/소스/문서 :::

강좌/소스/문서 성격에 맞지 않는 광고,비방,질문의 글은 즉시 삭제하며
내용을 복사하여 사용할 경우 반드시 이곳(http://www.howto.pe.kr)을 출처로 명시하여 주세요


Category

  김영대(2003-03-17 02:01:50, Hit : 4567, Vote : 1236
 [Thread] pthread_setcanceltype() function

/***************************************************
*--------------------------------------------------
*   Creat  : 2001. 04. 10  (programed by Cori-Young )
*   Site: http://www.byoneself.co.kr
*   Update :
*--------------------------------------------------
*   Compile : cc -o xxx xxx.c -lpthread -lrt
*--------------------------------------------------
* Machine hardware:   sun4u
* OS version:         5.7
* Processor type:     sparc
* Hardware:           SUNW,Ultra-60
***************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>

#define _MULTI_THREADED

static void checkResults(char *string, int rc) {

  if (rc) {
    printf("Error on : %s, rc=%d", string, rc);
    exit(EXIT_FAILURE);
  }
  return;
}

pthread_mutex_t    mutex = PTHREAD_MUTEX_INITIALIZER;

void cleanupHandler(void *parm)
{

  int rc;

  printf("Inside cleanup handler, unlock mutexn");

  rc = pthread_mutex_unlock((pthread_mutex_t *)parm);
  checkResults("pthread_mutex_unlockn", rc);
}

void *threadfunc(void *parm)
{

  int            rc;
  int            oldtype;

  printf("Entered secondary thread, lock mutexn");

  rc = pthread_mutex_lock(&mutex);
  checkResults("pthread_mutex_lock()n", rc);
  pthread_cleanup_push(cleanupHandler, &mutex);

  /* We must assume there's a good reason for async. cancellability  */
  /* and also, we must assume that if we get interrupted, its        */
  /* appropriate to unlock the mutex. More than likely it isn't      */
  /* because we'll have left some data structures in a strange state */
  /* if we're async. interrupted while holding the mutex             */

  rc = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
  checkResults("pthread_setcanceltype()n", rc);

  printf("Secondary thread is now loopingn");

  while (1) { sleep(1); }

  printf("Unexpectedly got out of loop!n");
  pthread_cleanup_pop(0);
  return NULL;
}

int main(int argc, char **argv)
{

  pthread_t             thread;
  int                   rc=0;
  void                 *status;

  printf("Enter Testcase - %sn", argv[0]);
  /* Create a thread using default attributes */
  printf("Create thread using the NULL attributesn");

  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create(NULL)n", rc);

  /* sleep() isn't a very robust way to wait for the thread */
  sleep(1);

  printf("Cancel the threadn");

  rc = pthread_cancel(thread);
  checkResults("pthread_cancel()n", rc);

  rc = pthread_join(thread, &status);

  if (status != PTHREAD_CANCELED) {
    printf("Unexpected thread statusn");
    exit(1);
  }

  printf("Main completedn");
  return 0;
}





42   [Unix/Linux] [Thread] pthread_muxtexcond() function  김영대 2003/03/17 5094 1383
41   [Unix/Linux] [Thread] pthread_rwlock_init() function  김영대 2003/03/17 4846 1241
40   [Unix/Linux] [Thread] pthread_rwlock_init() function  김영대 2003/03/17 5092 1312
39   [Unix/Linux] [Thread] pthread_self() function  김영대 2003/03/17 5148 1466
38   [Unix/Linux] [Thread] pthread_join() function  김영대 2003/03/17 4831 1223
37   [Unix/Linux] [Thread] pthread_mutex_unlock() function  김영대 2003/03/17 4579 1248
36   [Unix/Linux] [Thread] pthread_mutex_lock() function  김영대 2003/03/17 4677 1190
35   [Unix/Linux] [Thread] pthread_mutex_trylock() function  김영대 2003/03/17 4524 1048
34   [Unix/Linux] [Thread] pthread_mutexattr_destroy() function  김영대 2003/03/17 4490 1197
33   [Unix/Linux] [Thread] pthread_mutexattr_init() function  김영대 2003/03/17 5830 1580
32   [Unix/Linux] [Thread] pthread_mutex_init() function  김영대 2003/03/17 5375 1402
31   [Unix/Linux] [Thread] pthread_key_delete() function  김영대 2003/03/17 4464 1231
30   [Unix/Linux] [Thread] pthread_key_create() function  김영대 2003/03/17 5180 1362
29   [Unix/Linux] [Thread] pthread_setschedparam() function  김영대 2003/03/17 9869 1706
  [Unix/Linux] [Thread] pthread_setcanceltype() function  김영대 2003/03/17 4567 1236
27   [Unix/Linux] [Thread] pthread_setcancelstate() function  김영대 2003/03/17 4848 1305
26   [Unix/Linux] [Thread] pthread_once() function  김영대 2003/03/17 5688 1541
25   [Unix/Linux] [Thread] pthread_getschedparam() function  김영대 2003/03/17 4700 1349
24   [Unix/Linux] [Thread] pthread_exit() function  김영대 2003/03/17 4664 1327
23   [Unix/Linux] [Thread] pthread_equal() function  김영대 2003/03/17 4828 1371

[1] 2 [3][4]
 

Copyright 1999-2020 Zeroboard / skin by zero