- 論壇徽章:
- 0
|
本帖最后由 chen1922 于 2012-07-30 15:17 編輯
=============參考內(nèi)核的宏==================
- struct list_head{
- struct list_head *next;
- struct list_head *prev;
- };
- #define list_entry(ptr, type, member) ({\
- const typeof(((type *)0)->member) * __mptr = (ptr); \
- (type *)((char *)__mptr - offsetof(type, member)); \
- })
- #define offsetof(TYPE, MEMBER) ((char *)&((TYPE *)0)->MEMBER)
復(fù)制代碼 =========================================
自定義的線程池結(jié)構(gòu)
- typedef struct{ /* 線程池 */
- pthread_t thread_id; /* 線程ID */
- unsigned long thread_count; /* 線程處理數(shù) */
- int flags; /* 線程狀態(tài) */
- struct list_head list;
- }Thread_pool;
復(fù)制代碼 函數(shù)調(diào)用:
- void thread_make(struct list_head *head)
- {
- Thread_pool *tp;
- tp = list_entry(&head->next, Thread_pool, list);
- pthread_create(&tp->thread_id, NULL, doit, (void *)tp);
- }
復(fù)制代碼 gcc -E 后內(nèi)容如下:
- void thread_make(struct list_head *head)
- {
- Thread_pool *tp;
- tp = ({ const typeof(((Thread_pool *)0)->list) * __mptr = (&head->next);
- (Thread_pool *)((char *)__mptr - ((char *)&((Thread_pool *)0)->list));
- });
- pthread_create(&tp->thread_id, ((void *)0), doit, (void *)tp);
- }
復(fù)制代碼 我看了半天還是沒發(fā)現(xiàn)有啥問題,但是編譯器警告。。。。請指教{:3_188:}
thread_make.c: 在函數(shù)‘thread_make’中:
thread_make.c:12:11: 警告: 從不兼容的指針類型初始化 [默認(rèn)啟用] |
|