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

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


Category

  김영대(2003-03-17 01:47:13, Hit : 6033, Vote : 1783
 [Thread] pthread_cond_timedwait() 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
#define NTHREADS                3
#define WAIT_TIME_SECONDS       15

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

  if (rc) {

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

int                 workToDo = 0;
pthread_cond_t      cond  = PTHREAD_COND_INITIALIZER;
pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;

void *threadfunc(void *parm)
{
  int               rc;
  struct timespec   ts;
  struct timeval    tp;

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

  /* Usually worker threads will loop on these operations */

  while (1) {

    rc =  gettimeofday(&tp, NULL);
    checkResults("gettimeofday()n", rc);

    /* Convert from timeval to timespec */
    ts.tv_sec  = tp.tv_sec;
    ts.tv_nsec = tp.tv_usec * 1000;
    ts.tv_sec += WAIT_TIME_SECONDS;

    while (!workToDo) {

      printf("Thread blockedn");

      rc = pthread_cond_timedwait(&cond, &mutex, &ts);
      /* If the wait timed out, in this example, the work is complete, and   */
      /* the thread will end.                                                */
      /* In reality, a timeout must be accompanied by some sort of checking  */
      /* to see if the work is REALLY all complete. In the simple example    */
      /* we'll just go belly up when we time out.                            */

      if (rc == ETIMEDOUT) {
        printf("Wait timed out!n");

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

        pthread_exit(NULL);

      }

      checkResults("pthread_cond_timedwait()n", rc);
    }
    printf("Thread consumes work heren");
    workToDo = 0;

  }

  rc = pthread_mutex_unlock(&mutex);
  checkResults("pthread_mutex_lock()n", rc);
  return NULL;
}



int main(int argc, char **argv)

{

  int                   rc=0;

  int                   i;

  pthread_t             threadid[NTHREADS];



  printf("Enter Testcase - %sn", argv[0]);



  printf("Create %d threadsn", NTHREADS);

  for(i=0; i<NTHREADS; ++i) {

    rc = pthread_create(&threadid[i], NULL, threadfunc, NULL);

    checkResults("pthread_create()n", rc);

  }



  rc = pthread_mutex_lock(&mutex);

  checkResults("pthread_mutex_lock()n", rc);



  printf("One work item to give to a threadn");

  workToDo = 1;

  rc = pthread_cond_signal(&cond);

  checkResults("pthread_cond_signal()n", rc);



  rc = pthread_mutex_unlock(&mutex);

  checkResults("pthread_mutex_unlock()n", rc);



  printf("Wait for threads and cleanupn");

  for (i=0; i<NTHREADS; ++i) {

    rc = pthread_join(threadid[i], NULL);

    checkResults("pthread_join()n", rc);

  }



  pthread_cond_destroy(&cond);

  pthread_mutex_destroy(&mutex);

  printf("Main completedn");

  return 0;

}





43   [Unix/Linux] [Thread] pthread_equal() function  김영대 2003/03/17 4837 1372
42   [Unix/Linux] [Thread] pthread_detach() function  김영대 2003/03/17 5260 1426
41   [Unix/Linux] [Thread] pthread_create() function  김영대 2003/03/17 5111 1352
40   [Unix/Linux] [Thread] pthread_cond_wait() function  김영대 2003/03/17 4835 1323
  [Unix/Linux] [Thread] pthread_cond_timedwait() function  김영대 2003/03/17 6033 1783
38   [Unix/Linux] [Thread] pthread_cond_signal() function  김영대 2003/03/17 4333 1193
37   [Unix/Linux] [Thread] pthread_cond_init() function  김영대 2003/03/17 5548 1514
36   [Unix/Linux] [Thread] pthread_cond_destroy() function  김영대 2003/03/17 4681 1243
35   [Unix/Linux] [Thread] pthread_cond_broadcast() function  김영대 2003/03/17 4807 1394
34   [Unix/Linux] [Thread] pthread_condattr_destroy() function  김영대 2003/03/17 4670 1348
33   [Unix/Linux] [Thread] pthread_cond() function  김영대 2003/03/17 6145 1629
32   [Unix/Linux] [Thread] pthread_cleanup_push() function  김영대 2003/03/17 5007 1446
31   [Unix/Linux] [Thread] pthread_cleanup_pop() function  김영대 2003/03/17 5010 1479
30   [Unix/Linux] [Thread] pthread_cancel() function  김영대 2003/03/17 5957 1699
29   [Unix/Linux] [Thread] pthread_attr_setinheritsched() function  김영대 2003/03/17 5185 1455
28   [Unix/Linux] [Thread] pthread_attr_setdetachstate() function  김영대 2003/03/17 5403 1539
27   [Unix/Linux] [Thread] pthread_attr_init() function  김영대 2003/03/17 4860 1382
26   [Unix/Linux] [Thread] pthread_attr_getschedparam() function  김영대 2003/03/17 4999 1413
25   [Unix/Linux] [Thread] pthread_attr_getinheritsched() function  김영대 2003/03/17 5052 1559
24   [Unix/Linux] [Thread] pthread_attr_getdetachstate() function  김영대 2003/03/17 5113 1490

[1][2][3][4][5][6][7] 8 [9][10]
 

Copyright 1999-2020 Zeroboard / skin by zero