[英]What is the difference between normal function call and signal() system call?
[英]What is the difference between a thread entry function and a normal function?
我想知道線程輸入函數之間的區別:
void* thread_function (void* parameter)
{
struct parameter * thread_data = (struct parameter *)parameter;
char buffer[20];
int temp;
printf_buffer(buffer);
}
和一個正常的功能:
void printf_buffer(char *buffer)
{
printf("buffer is %s",buffer);
return;
}
我知道創建線程時會調用線程條目,以及如何使用正常功能。
在執行,行為或創建實例方面,線程入口函數與普通函數之間是否還有其他區別?
在您所說的“線程函數”(盡管賈斯汀編輯過稱其為“線程入口函數”)和您所說的“普通函數”之間,語言沒有區別。
使用pthreads,線程的所謂“啟動例程”是一個函數,它使用單個void*
參數並返回void*
,但是沒有什么可以阻止您“正常”調用同一函數。
當線程的啟動例程返回時,線程完成執行,但這僅僅是因為線程實現調用了它,然后完成了線程。 這並不是因為啟動例程本身有什么特別之處。
線程函數只是線程的入口/出口點。 該功能的執行與您所說的普通功能沒有什么不同。
man pthread_create
很好地定義了它:
http://linux.die.net/man/3/pthread_create
尚未提及的一個主要區別是線程條目應該期望在調用者的堆棧以外的堆棧上進行操作。 出於這個原因,當線程分離時,您通常希望將資源的堆副本作為參數傳遞,然后在條目中釋放它:
// the caller must pass a heap copy of struct parameter* arg
void* detached_entry(void* arg) {
struct parameter* parameter = (struct parameter*)arg;
...
free(parameter);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.