Chinaunix

标题: sunos下编译C++报错 [打印本页]

作者: chllcy88    时间: 2012-03-21 20:02
标题: sunos下编译C++报错
编译的是Programming With POSIXThreads 的源代码.

报错信息如下:
CC -lpthread -g    -o alarm_cond alarm_cond.c
未定义                  文件中的
符号                       在文件中
main                                /opt/sunstudio12.1/prod/lib/crt1.o
ld: 致命的: 符号参照错误. 没有输出被写入alarm_cond
*** Error code 1
make: Fatal error: Command failed for target `alarm_cond'
  1. /*
  2. * alarm_cond.c
  3. *
  4. * This is an enhancement to the alarm_mutex.c program, which
  5. * used only a mutex to synchronize access to the shared alarm
  6. * list. This version adds a condition variable. The alarm
  7. * thread waits on this condition variable, with a timeout that
  8. * corresponds to the earliest timer request. If the main thread
  9. * enters an earlier timeout, it signals the condition variable
  10. * so that the alarm thread will wake up and process the earlier
  11. * timeout first, requeueing the later request.
  12. */
  13. #include <pthread.h>
  14. #include <time.h>
  15. #include "errors.h"

  16. /*
  17. * The "alarm" structure now contains the time_t (time since the
  18. * Epoch, in seconds) for each alarm, so that they can be
  19. * sorted. Storing the requested number of seconds would not be
  20. * enough, since the "alarm thread" cannot tell how long it has
  21. * been on the list.
  22. */
  23. typedef struct alarm_tag {
  24.     struct alarm_tag    *link;
  25.     int                 seconds;
  26.     time_t              time;   /* seconds from EPOCH */
  27.     char                message[64];
  28. } alarm_t;

  29. pthread_mutex_t alarm_mutex = PTHREAD_MUTEX_INITIALIZER;
  30. pthread_cond_t alarm_cond = PTHREAD_COND_INITIALIZER;
  31. alarm_t *alarm_list = NULL;
  32. time_t current_alarm = 0;

  33. /*
  34. * Insert alarm entry on list, in order.
  35. */
  36. void alarm_insert (alarm_t *alarm)
  37. {
  38.     int status;
  39.     alarm_t **last, *next;

  40.     /*
  41.      * LOCKING PROTOCOL:
  42.      *
  43.      * This routine requires that the caller have locked the
  44.      * alarm_mutex!
  45.      */
  46.     last = &alarm_list;
  47.     next = *last;
  48.     while (next != NULL) {
  49.         if (next->time >= alarm->time) {
  50.             alarm->link = next;
  51.             *last = alarm;
  52.             break;
  53.         }
  54.         last = &next->link;
  55.         next = next->link;
  56.     }
  57.     /*
  58.      * If we reached the end of the list, insert the new alarm
  59.      * there.  ("next" is NULL, and "last" points to the link
  60.      * field of the last item, or to the list header.)
  61.      */
  62.     if (next == NULL) {
  63.         *last = alarm;
  64.         alarm->link = NULL;
  65.     }
  66. #ifdef DEBUG
  67.     printf ("[list: ");
  68.     for (next = alarm_list; next != NULL; next = next->link)
  69.         printf ("%d(%d)[\"%s\"] ", next->time,
  70.             next->time - time (NULL), next->message);
  71.     printf ("]\n");
  72. #endif
  73.     /*
  74.      * Wake the alarm thread if it is not busy (that is, if
  75.      * current_alarm is 0, signifying that it's waiting for
  76.      * work), or if the new alarm comes before the one on
  77.      * which the alarm thread is waiting.
  78.      */
  79.     if (current_alarm == 0 || alarm->time < current_alarm) {
  80.         current_alarm = alarm->time;
  81.         status = pthread_cond_signal (&alarm_cond);
  82.         if (status != 0)
  83.             err_abort (status, "Signal cond");
  84.     }
  85. }

  86. /*
  87. * The alarm thread's start routine.
  88. */
  89. void *alarm_thread (void *arg)
  90. {
  91.     alarm_t *alarm;
  92.     struct timespec cond_time;
  93.     time_t now;
  94.     int status, expired;

  95.     /*
  96.      * Loop forever, processing commands. The alarm thread will
  97.      * be disintegrated when the process exits. Lock the mutex
  98.      * at the start -- it will be unlocked during condition
  99.      * waits, so the main thread can insert alarms.
  100.      */
  101.     status = pthread_mutex_lock (&alarm_mutex);
  102.     if (status != 0)
  103.         err_abort (status, "Lock mutex");
  104.     while (1) {
  105.         /*
  106.          * If the alarm list is empty, wait until an alarm is
  107.          * added. Setting current_alarm to 0 informs the insert
  108.          * routine that the thread is not busy.
  109.          */
  110.         current_alarm = 0;
  111.         while (alarm_list == NULL) {
  112.             status = pthread_cond_wait (&alarm_cond, &alarm_mutex);
  113.             if (status != 0)
  114.                 err_abort (status, "Wait on cond");
  115.             }
  116.         alarm = alarm_list;
  117.         alarm_list = alarm->link;
  118.         now = time (NULL);
  119.         expired = 0;
  120.         if (alarm->time > now) {
  121. #ifdef DEBUG
  122.             printf ("[waiting: %d(%d)\"%s\"]\n", alarm->time,
  123.                 alarm->time - time (NULL), alarm->message);
  124. #endif
  125.             cond_time.tv_sec = alarm->time;
  126.             cond_time.tv_nsec = 0;
  127.             current_alarm = alarm->time;
  128.             while (current_alarm == alarm->time) {
  129.                 status = pthread_cond_timedwait (
  130.                     &alarm_cond, &alarm_mutex, &cond_time);
  131.                 if (status == ETIMEDOUT) {
  132.                     expired = 1;
  133.                     break;
  134.                 }
  135.                 if (status != 0)
  136.                     err_abort (status, "Cond timedwait");
  137.             }
  138.             if (!expired)
  139.                 alarm_insert (alarm);
  140.         } else
  141.             expired = 1;
  142.         if (expired) {
  143.             printf ("(%d) %s\n", alarm->seconds, alarm->message);
  144.             free (alarm);
  145.         }
  146.     }
  147.     return NULL;
  148. }

  149. int main (int argc, char *argv[])
  150. {
  151.     int status;
  152.     char line[128];
  153.     alarm_t *alarm;
  154.     pthread_t thread;

  155.     status = pthread_create (
  156.         &thread, NULL, alarm_thread, NULL);
  157.     if (status != 0)
  158.         err_abort (status, "Create alarm thread");
  159.     while (1) {
  160.         printf ("Alarm> ");
  161.         if (fgets (line, sizeof (line), stdin) == NULL) exit (0);
  162.         if (strlen (line) <= 1) continue;
  163.         alarm = (alarm_t*)malloc (sizeof (alarm_t));
  164.         if (alarm == NULL)
  165.             errno_abort ("Allocate alarm");

  166.         /*
  167.          * Parse input line into seconds (%d) and a message
  168.          * (%64[^\n]), consisting of up to 64 characters
  169.          * separated from the seconds by whitespace.
  170.          */
  171.         if (sscanf (line, "%d %64[^\n]",
  172.             &alarm->seconds, alarm->message) < 2) {
  173.             fprintf (stderr, "Bad command\n");
  174.             free (alarm);
  175.         } else {
  176.             status = pthread_mutex_lock (&alarm_mutex);
  177.             if (status != 0)
  178.                 err_abort (status, "Lock mutex");
  179.             alarm->time = time (NULL) + alarm->seconds;
  180.             /*
  181.              * Insert the new alarm into the list of alarms,
  182.              * sorted by expiration time.
  183.              */
  184.             alarm_insert (alarm);
  185.             status = pthread_mutex_unlock (&alarm_mutex);
  186.             if (status != 0)
  187.                 err_abort (status, "Unlock mutex");
  188.         }
  189.     }
  190. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2