英語不會果然是硬傷啊,英文的看了半天,就是一點都看不懂啊,下面來用中文解釋下這個函數把,汗
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 #include<stdio.h> #include<pthread.h> void *print_thread_id(void *arg) { /* 打印當前線程的線程號*/ printf("Current thread id is %u\n", (unsigned)pthread_self()); } int main(int argc, char *argv[]) { pthread_t thread; /*保存線程號*/ /*創建一個線程 */ pthread_create(&thread, NULL, print_thread_id, NULL); sleep(1); /*休眠1s*/ /*打印進程號 */ printf("Main thread id is %u\n", (unsigned)pthread_self()); return 0; }
編譯的時候,一定要加上-lpthread選項,不然會報錯:undefined reference to `pthread_create'。
下面來看看pthread_create的聲明:
#include<pthread.h>
int pthread_create(pthread_t *thread, pthread_addr_t *arr,
void* (*start_routine)(void *), void *arg);
?
- ?thread :用于返回創建的線程的ID
- arr ? : 用于指定的被創建的線程的屬性,上面的函數中使用NULL,表示使用默認的屬性
- start_routine?? : 這是一個函數指針,指向線程被創建后要調用的函數
- arg : 用于給線程傳遞參數,在本例中沒有傳遞參數,所以使用了NULL, 這個參數傳遞給start_routine();
?
? 線程相對進程來說,有幾大優點,一是其切換速度快,其保存現場花費的時間比進程少得多,二是:線程間的同步比進程簡單(至少我是這樣認為的)。當然,可能還有很多其他的優點我沒有發現,還請您多多指教。
轉自: http://www.cnblogs.com/huangwei/archive/2010/05/19/1739593.html
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
