2016-09-22 112 views
0

我在线程内创建线程时遇到问题。我需要创建thread1和thread1做“某事”,以及创建thread2,这将做其他事情。如何创建一个在C编程中创建另一个线程的线程?

我的代码:

#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; 


} 

回答

3

创建从一个线程回调内部线程没有从主线程创建它们的不同。当然,每个线程都需要自己的回调函数 - 这是用给定的pthreads格式声明的,void* func (void*)

由于未知原因,您尝试在另一个函数内声明一个函数。这没有任何意义,并且不允许使用C线程或不使用线程。

如果你想限制第二个线程的范围,然后把两个线程的回调属于自己的模块中,并进行第二次回调函数static。这是非常基本的程序设计 - 在推出多线程之前,我建议您先研究一下。

+2

从技术上讲,应该有相同行为的两个线程当然可以运行相同的线程函数。 – unwind