簡體   English   中英

如何創建一個線程,該線程在C編程中創建另一個線程?

[英]How do you create a thread that create another thread in C programing?

我在線程中創建線程時遇到問題。 我需要創建thread1,thread1可以執行“某些操作”,還需要創建thread2來執行其他操作。

我的代碼:

    #include <pthread.h>
   #include <stdio.h>
   #include <errno.h>
   #include <stdlib.h>
   #include <unistd.h>

void *msg1(void *ignored)
{

void *msg2(void *ignored)
{
printf("this is thread2");
}


pthread_t thread;
int thread2;
thread2 = pthread_create(&thread, NULL, msg2, NULL);

return 0;
}



int main () 
{
pthread_t thread;
int thread1;
thread1 = pthread_create(&thread, NULL, msg1, NULL);
return 1;


}

從線程回調內部創建線程與從主線程創建線程沒有什么不同。 自然地,每個線程將需要其自己的回調函數-該函數以給定的pthreads格式void* func (void*)

由於未知的原因,您嘗試在另一個函數中聲明一個函數。 這沒有任何意義,並且在C.線程或無線程中是不允許的。

如果希望限制第二個線程的范圍,則將兩個線程回調都放在各自的模塊中,並使第二個回調函數為static 這是非常基礎的程序設計-在進行多線程處理之前,我建議您進行長時間的研究。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM