= 目標 = * 以 gcc 編譯 spin_or_mutex.c * 編輯configure.ac makefile.am == spin_or_mutex.c == {{{ #!c //export LIBRARY_PATH=/usr/lib/i386-linux-gnu/ #include #include #include #include #include #include #define LOOPS 10000000 #ifdef USE_SPINLOCK pthread_spinlock_t spinlock; #else pthread_mutex_t mutex; #endif pid_t gettid() { return syscall( __NR_gettid ); } char data[LOOPS]; void *consumer(void *ptr) { int i = 0; printf("Consumer TID %lu\n", (unsigned long)gettid()); while (1) { #ifdef USE_SPINLOCK pthread_spin_lock(&spinlock); #else pthread_mutex_lock(&mutex); #endif if (i > LOOPS) { #ifdef USE_SPINLOCK pthread_spin_unlock(&spinlock); #else pthread_mutex_unlock(&mutex); #endif break; } data[i] = 0; i++; #ifdef USE_SPINLOCK pthread_spin_unlock(&spinlock); #else pthread_mutex_unlock(&mutex); #endif } return NULL; } int main() { int i; pthread_t thr1, thr2; struct timeval tv1, tv2; #ifdef USE_SPINLOCK pthread_spin_init(&spinlock, 0); #else pthread_mutex_init(&mutex, NULL); #endif // Creating the list content... for (i = 0; i < LOOPS; i++){ data[i] = 1; } // Measuring time before starting the threads... gettimeofday(&tv1, NULL); pthread_create(&thr1, NULL, consumer, NULL); pthread_create(&thr2, NULL, consumer, NULL); pthread_join(thr1, NULL); pthread_join(thr2, NULL); // Measuring time after threads finished... gettimeofday(&tv2, NULL); if (tv1.tv_usec > tv2.tv_usec) { tv2.tv_sec--; tv2.tv_usec += 1000000; } printf("Result - %ld.%ld\n", tv2.tv_sec - tv1.tv_sec, tv2.tv_usec - tv1.tv_usec); #ifdef USE_SPINLOCK pthread_spin_destroy(&spinlock); #else pthread_mutex_destroy(&mutex); #endif return 0; } }}} == 以 gcc 編譯 spin_or_mutex.c == === 編輯Makefile === {{{ #!text all: gcc spin_or_mutex.c -o spin_or_mutex clean: rm -f spin_or_mutex }}} == 執行 == {{{ $ make gcc spin_or_mutex.c -o spin_or_mutex /tmp/ccTHYKpZ.o: In function `main': spin_or_mutex.c:(.text+0xf2): undefined reference to `pthread_create' spin_or_mutex.c:(.text+0x116): undefined reference to `pthread_create' spin_or_mutex.c:(.text+0x12a): undefined reference to `pthread_join' spin_or_mutex.c:(.text+0x13e): undefined reference to `pthread_join' collect2: ld returned 1 exit status make: *** [all] Error 1 }}} === 修改 Makefile === {{{ #!text all: gcc -lpthread spin_or_mutex.c -o spin_or_mutex clean: rm -f spin_or_mutex }}} == 執行 == {{{ $ make $ ./spin_or_mutex }}} * Done! == 編輯configure.ac makefile.am == == 執行 == {{{ $ autoscan $ mv configure.scan configure.ac $ mv Makefile Makefile.in $ autoconf $ autoheader $ ./configure $ make clean }}} == 修改spin_or_mutex.c == {{{ #!c #include "config.h" // 以下略 }}} == 新增Makefile.am == {{{ #!text bin_PROGRAMS=spin_or_mutex spin_or_mutex_SOURCES=spin_or_mutex.c }}} == 產生make檔 == {{{ $ aclocal $ autoconf $ automake }}} * 修改error {{{ #!text AM_CONDITIONAL AC_PROG_CC AC_PROG_CXX AC_PROG_OBJC AM_PROG_AS AM_PROG_GCJ AM_PROG_UPC }}} * ./confifure {{{ $ ./configure $ make make all-am make[1]: Entering directory `/home/shunfa/test/P1_Thomas' source='spin_or_mutex.c' object='spin_or_mutex.o' libtool=no \ DEPDIR=.deps depmode=none /bin/bash ./depcomp \ gcc -DHAVE_CONFIG_H -I. -g -O2 -c spin_or_mutex.c gcc -g -O2 -o spin_or_mutex spin_or_mutex.o -lpthread spin_or_mutex.o: In function `_start': (.text+0x0): multiple definition of `_start' /usr/lib/gcc/i486-linux-gnu/4.4.5/../../../../lib/crt1.o:(.text+0x0): first defined here spin_or_mutex.o:(.rodata+0x0): multiple definition of `_fp_hw' /usr/lib/gcc/i486-linux-gnu/4.4.5/../../../../lib/crt1.o:(.rodata+0x0): first defined here spin_or_mutex.o: In function `_fini': (.fini+0x0): multiple definition of `_fini' /usr/lib/gcc/i486-linux-gnu/4.4.5/../../../../lib/crti.o:(.fini+0x0): first defined here spin_or_mutex.o:(.rodata+0x4): multiple definition of `_IO_stdin_used' /usr/lib/gcc/i486-linux-gnu/4.4.5/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here spin_or_mutex.o: In function `__data_start': (.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/i486-linux-gnu/4.4.5/../../../../lib/crt1.o:(.data+0x0): first defined here spin_or_mutex.o: In function `__data_start': (.data+0x4): multiple definition of `__dso_handle' /usr/lib/gcc/i486-linux-gnu/4.4.5/crtbegin.o:(.data+0x0): first defined here spin_or_mutex.o: In function `_init': (.init+0x0): multiple definition of `_init' /usr/lib/gcc/i486-linux-gnu/4.4.5/../../../../lib/crti.o:(.init+0x0): first defined here /usr/lib/gcc/i486-linux-gnu/4.4.5/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__' spin_or_mutex.o:(.dtors+0x4): first defined here collect2: ld returned 1 exit status make[1]: *** [spin_or_mutex] Error 1 make[1]: Leaving directory `/home/shunfa/test/P1_Thomas' make: *** [all] Error 2 }}}