繁体   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