心得体会
IDE自动提示补全真的特别重要,大大提高开发效率。通过IDE自动搜索库函数API。
GDB调试能力要加强。
使用前面提供的list.h来改写wordCount程序,头文件的实现相当有技巧,将使用外部list库,多线程都用宏定义到同一份代码中,这是C/C++开发的常用技巧。
这里Linux内核里面的链表实现和传统的有一些区别
word_count.h
#ifdef PINTOS_LIST
#include "list.h"
typedef struct word_count {
char* word;
int count;
struct list_elem elem;
} word_count_t;
#ifdef PTHREADS
#include <pthread.h>
typedef struct word_count_list {
struct list lst;
pthread_mutex_t lock;
} word_count_list_t;
#else /* PTHREADS */
typedef struct list word_count_list_t;
#endif /* PTHREADS */
#else /* PINTOS_LIST */
typedef struct word_count {
char* word;
int count;
struct word_count* next;
} word_count_t;
typedef word_count_t* word_count_list_t;
#endif /* PINTOS_LIST */
/* Initialize a word count list. */
void init_words(word_count_list_t* wclist);
/* Get length of a word count list. */
size_t len_words(word_count_list_t* wclist);
/* Find a word in a word_count list. */
word_count_t* find_word(word_count_list_t* wclist, char* word);
/*
* Insert word with count=1, if not already present; increment count if
* present. Takes ownership of word.
*/
word_count_t* add_word(word_count_list_t* wclist, char* word);
/* Print word counts to a file. */