# 2016q3 Homework2 (phonebook_concurrent) contributed by <`cjTsai3030`> ### Concurrency 資料閱讀 * 在看過[The free lunch is over](http://www.gotw.ca/publications/concurrency-ddj.htm)後,應該是在說明目前硬體的優化已經有瓶頸存在,再繼續往下做的話,設計者可能會為了效能,有些程式正確性的問題就會產生,因此為了要再優化,開始朝向軟體部份的修改,就提到了Concurrency 的概念。 * Rob Pike 的影片有說到關於Concurrency 的概念,就我了解,應該是希望可以將程式分解成多個小步驟,每個小步驟都可以由不同的thread 執行,並且會希望每一個步驟之間都是獨立的。 ### pthread_create ```clike= int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); ``` * pthread_t *thread : 指定要使用那一條thread * const pthread_attr_t *attr : thread在建立的時候會需要一個 pthread_attr_t 的 structure 內部的[attribute](http://man7.org/linux/man-pages/man7/attributes.7.html),若為NULL則使用default。 * void *(*start_routine) (void *) : 決定thread 要執行的事情(function append) * void *arg : 在 start_routine 可能會需要某一些的 input ,這時候無法直接給值,要藉由thread 的 arugument 這個參數,將需要的 input 轉成 void * 的型態才可以使用。 * 在原來的code 中,一開始不太了解為什麼會是傳入 append_a,有特別列出需要再了解。 ### Refactoring * 修改原本的code,結合一些相關 for loop的運作,提升code 的可讀性 * 有些 function 及變數的使用很類似但是沒有用比較明顯的差別做變數宣告也一並改變 * 原來的版本會有資料找不到的問題,做好輸出正確性的修正。  >> 光說不練是不行的,show me the code! [name=jserv] >> 已補上 github 連結[name=cjTsai3030] >> 趕快跟上進度!請見: [弱是罪惡](http://folkdoc.com/classic/p02/ba/ba07/04.htm) [name=jserv] >> 會再更努力補上進度[name=cjTsai3030] <s>pthread_create arg 用處?</s> >> 讀第一手資料: [pthread_create](http://man7.org/linux/man-pages/man3/pthread_create.3.html),然後拿程式來改! [name=jserv] >>謝謝老師,會再補上說明。[name=cjTsai3030] ### Thread pool * 參考了[threadpool-mbrossad](https://github.com/mbrossard/threadpool)和 [C-Thread-Pool](https://github.com/Pithikos/C-Thread-Pool)兩個提供的資源,前者有提供 work queue pull 的函式而後者沒有可直接使用的函式,有嘗試去寫出缺少所需的功能,但是<s>沒有寫出來</s>->能力不足,後來參考[andy19950](https://github.com/andy19950/phonebook-concurrent)及 [TempoJiJi](https://github.com/TempoJiJi/phonebook-concurrent)的方式完成。 * 但是實作工作分配的方式與一開始的程式一模一樣,因此再速度上沒有明顯的提升。  * 嘗試其他工作分配方式中...
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up