::: °­ÁÂ/¼Ò½º/¹®¼­ :::

°­ÁÂ/¼Ò½º/¹®¼­ ¼º°Ý¿¡ ¸ÂÁö ¾Ê´Â ±¤°í,ºñ¹æ,Áú¹®ÀÇ ±ÛÀº Áï½Ã »èÁ¦Çϸç
³»¿ëÀ» º¹»çÇÏ¿© »ç¿ëÇÒ °æ¿ì ¹Ýµå½Ã ÀÌ°÷(http://www.howto.pe.kr)À» Ãâó·Î ¸í½ÃÇÏ¿© ÁÖ¼¼¿ä


Category

  ±è¿µ´ë(2003-03-17 01:47:13, Hit : 11163, Vote : 1895
 [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;

}





62   [Unix/Linux] [¼Ò½º] top for SunOS 5.x (Solaris 2.x)  ±è¿µ´ë 2004/02/20 15503 1343
61   [Unix/Linux] [°­ÁÂ] ÀÎÅÚ ¼¾Æ®¸®³ë ¹«¼±·£ Ä«µå: ndiswrapper  ±è¿µ´ë 2004/06/27 12587 2196
60   [Unix/Linux] [POSIX IPC] mq_send() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 12537 1737
59   [Unix/Linux] [System V IPC] shmget() function - °øÀ¯¸Þ¸ð¸®  ±è¿µ´ë 2003/03/17 11292 1820
58   [Unix/Linux] [¼Ò½º] top for System V Release 4, Intel or Sparc CPU  ±è¿µ´ë 2004/02/20 11234 1394
  [Unix/Linux] [Thread] pthread_cond_timedwait() function  ±è¿µ´ë 2003/03/17 11163 1895
56   [Unix/Linux] [Thread] pthread_setschedparam() function  ±è¿µ´ë 2003/03/17 10960 1885
55   [Unix/Linux] [System V IPC] shmctl() function - °øÀ¯¸Þ¸ð¸®  ±è¿µ´ë 2003/03/17 9950 1744
54   [Unix/Linux] [°­ÁÂ] Apache + MySQL + PHP4 + Zend Optimizer ¼³Ä¡  ±è¿µ´ë 2003/04/15 9341 1689
53   [Unix/Linux] [°­ÁÂ] À¥·Î±×ºÐ¼®À» À§ÇÑ Webalizer + GDlib + PNGlib + Zlib ¼³Ä¡  ±è¿µ´ë 2003/05/04 8574 1527
52   [Unix/Linux] [System V IPC] msgsnd() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 8403 1938
51   [Unix/Linux] [POSIX IPC] mq_receive() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 8329 1841
50   [Unix/Linux] [POSIX IPC] »ý»êÀÚ/¼ÒºñÀÚ - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 8254 5717
49   [Unix/Linux] [¼Ò½º] String Çؽ¬(hash) ÇÔ¼ö  ±è¿µ´ë 2003/07/29 8119 1633
48   [Unix/Linux] [Thread] pthread_cancel() function  ±è¿µ´ë 2003/03/17 8103 3822
47   [Unix/Linux] [Thread] »ý»êÀÚ/¼ÒºñÀÚ 2  ±è¿µ´ë 2003/03/17 8084 1554
46   [Unix/Linux] [System V IPC] msgrcv() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 7586 1710
45   [Unix/Linux] [Thread] pthread_attr_getschedparam() function  ±è¿µ´ë 2003/03/17 7087 8237
44   [Unix/Linux] [Thread] Àаí/¾²±â  ±è¿µ´ë 2003/03/17 6909 1546
43   [Unix/Linux] [System V IPC] semget() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 6755 1676

1 [2][3][4]
 

Copyright 1999-2024 Zeroboard / skin by zero