pthread執行緒程式設計常用API

2021-06-22 14:40:03 字數 4603 閱讀 7181

自己寫共享控制

#include

#include #include #include //基本的共享變數互動

void *thread_function(void *arg);

int run_now = 1;

char message = "hello world";

int main()

while(print_count1++ < 20)

else

}printf("\nwaiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0)

printf("thread joined\n");

exit(exit_success);

}void *thread_function(void *arg)

else

}sleep(3);

}

訊號量的使用
#include #include #include #include #include #include void *thread_function(void *arg);

sem_t bin_sem;

#define work_size 1024

char work_area[work_size];

int main()

res = pthread_create(&a_thread, null, thread_function, null);

if (res != 0)

printf("input some text. enter 'end' to finish\n");

while(strncmp("end", work_area, 3) != 0)

printf("\nwaiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0)

printf("thread joined\n");

sem_destroy(&bin_sem);

exit(exit_success);

}void *thread_function(void *arg)

pthread_exit(null);

}

互斥量的使用
#include #include #include #include #include #include void *thread_function(void *arg);

pthread_mutex_t work_mutex; /* protects both work_area and time_to_exit */

#define work_size 1024

char work_area[work_size];

int time_to_exit = 0;

int main()

res = pthread_create(&a_thread, null, thread_function, null);

if (res != 0)

pthread_mutex_lock(&work_mutex);

printf("input some text. enter 'end' to finish\n");

while(!time_to_exit)

else }}

pthread_mutex_unlock(&work_mutex);

printf("\nwaiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0)

printf("thread joined\n");

pthread_mutex_destroy(&work_mutex);

exit(exit_success);

}void *thread_function(void *arg)

}time_to_exit = 1;

work_area[0] = '\0';

pthread_mutex_unlock(&work_mutex);

pthread_exit(0);

}

條件變數的使用
#include #include #include pthread_mutex_t mutex = pthread_mutex_initializer;/*初始化互斥鎖*/  

pthread_cond_t cond = pthread_cond_initializer;/*初始化條件變數*/

void *thread1(void *);

void *thread2(void *);

int i=1;

int main(void)

void *thread1(void *junk)

pthread_mutex_unlock(&mutex);/*解鎖互斥量*/

printf("thread1: unlock %d/n/n", __line__);

sleep(1);

} } void *thread2(void *junk)

pthread_mutex_unlock(&mutex);

printf("thread2: unlock %d/n/n", __line__);

sleep(1);

} }

執行緒屬性的設定
#include #include #include #include void *thread_function(void *arg);

char message = "hello world";

int thread_finished = 0;

int main()

res = pthread_attr_setdetachstate(&thread_attr, pthread_create_detached);

if (res != 0)

res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);

if (res != 0)

//while(!thread_finished)

(void)pthread_attr_destroy(&thread_attr);

printf("other thread finished, bye!\n");

exit(exit_success);

}void *thread_function(void *arg)

多執行緒的管理
#include #include #include #include #define num_threads 6 //建立6個執行緒

void *thread_function(void *arg);

int main()

sleep(1);

}printf("waiting for threads to finish...\n");

for(lots_of_threads = num_threads - 1; lots_of_threads >= 0; lots_of_threads--)

else

}printf("all done\n");

exit(exit_success);

}void *thread_function(void *arg)

執行緒的區域性儲存
#include #include //#include #include static pthread_key_t thread_log_key;

void write_to_thread_log( char const * message )

void close_thread_log( void * thread_log )

void * thread_function( void * args )

int main()

for ( i = 0; i < 5; ++i )

//注意close_thread_log將會在各個執行緒結束的時候執行,和delete無關。

pthread_key_delete(thread_log_key);

return 0;

}

Unix執行緒基礎程式設計pthread

程序fork的兩個問題 1.fork過於昂貴,需要把父程序的記憶體映像拷貝到子程序 2.fork返回之後父子程序之間資訊的傳遞需要程序間通訊 ipc 機制,子程序向父程序返回資訊較困難 執行緒可稱為輕權程序。同一程序內的不同執行緒共享相同的全域性記憶體,除此之外還共享 程序指令 大多數資料檔案 開啟...

pthread終止執行緒(linux程式設計)

linux終止執行緒 正常情況下,執行緒一旦建立便從指定函式開始執行直到該函式返回。一旦該函式返回,這些執行緒便自行終止。執行緒也可以在執行的途中通過呼叫pthread exit 終止自己的執行。實際上,非初始執行緒 這裡只main函式的執行緒 從開始函式返回是返回到執行緒庫中,由執行緒庫隱含地呼叫...

pthread多執行緒程式設計整理(二)

補充 在傳統的unix模型中,當乙個程序需要由另乙個實體執行某件事時,該程序派生 fork 乙個子程序,讓子程序去進行處理。unix下的大多數網路伺服器程式都是這麼編寫的,即父程序接受連線,派生子程序,子程序處理與客戶的互動。雖然這種模型很多年來使用得很好,但是fork時有一些問題 1.fork是昂...