简体   繁体   English

C中等待父线程输入时如何创建子线程和阻塞

[英]How to create Child Threads and block while waiting for input from the Parent thread in C

I am new to the community but I just got quick question about Threading.我是社区的新手,但我刚刚收到有关线程的快速问题。 So I am trying to create a Parent Thread that creates Six Child Threads.所以我试图创建一个创建六个子线程的父线程。 So what I have done so far is that, I've created a normal pthread_create which creates an thread but I am not quite sure how to create a child threads.所以到目前为止我所做的是,我创建了一个普通的 pthread_create,它创建了一个线程,但我不太确定如何创建子线程。

Creating a Parent Thread that creates Six Child Threads?创建一个创建六个子线程的父线程?

//Create a parent thread 
int main()
{ 
//I have created an array of thread to keep track of the threads in a data structure

   pthread_t parentThread[6];

   pthread_create(&parentThread[0], NULL, A, NULL);
    pthread_create(&parentThread[1], NULL, B, NULL);
    pthread_create(&parentThread[2], NULL, C, NULL);
    pthread_create(&parentThread[3], NULL, D, NULL);
    pthread_create(&parentThread[4], NULL, E, NULL);
    pthread_create(&parentThread[5], NULL, F, NULL);
 

}

Block while waiting for input from the Parent Thread在等待来自父线程的输入时阻塞

   //Do I just use pthread_join to block while for input from the parent thread? 
   pthread_create(&parentThread[0], NULL, A, NULL);
   pthread_join(parentThread[0], NULL); 
   pthread_create(&parentThread[1], NULL, B, NULL); 
   pthread_join(parentThread[1], NULL); 
   ...... 
   ......

For threading, when trying to block, you use pthread condition variables and mutexes.对于线程,当试图阻塞时,您可以使用 pthread 条件变量和互斥量。 You must lock a mutex so that threads can't access whatever you lock, then you need a condition variable so that threads will wait until the condition variables are altered.您必须锁定一个互斥锁,这样线程就无法访问您锁定的任何内容,然后您需要一个条件变量,以便线程将等待直到条件变量被更改。 You can think of condition variables as "wait channels".您可以将条件变量视为“等待通道”。 If you are trying to retain the data from a thread you can use join, but if not then I suggest using pthread_exit.如果你想保留线程中的数据,你可以使用 join,但如果不是,那么我建议使用 pthread_exit。 It's very hard to tell from your code, but you aren't creating a single parent thread.很难从您的代码中分辨出来,但您并没有创建单个父线程。 From the looks of it, you're creating a bunch of threads in main, which I assume are your "child threads".从外观上看,您正在 main 中创建一堆线程,我认为它们是您的“子线程”。 If you're trying to implement what I think, then you need a separate function that you will give to a single thread(parent thread) to execute (which I assume will create other threads).如果你想实现我的想法,那么你需要一个单独的 function ,你将把它交给一个线程(父线程)来执行(我假设它会创建其他线程)。 This function will have to return a void pointer to fit the parameters of the pthread_create function. I suggest reading the man page for pthread functions, because this code doesn't really give me the idea that you know what you're trying to implement.这个 function 将必须返回一个空指针以适应 pthread_create function 的参数。我建议阅读 pthread 函数的手册页,因为这段代码并没有真正让我知道你知道你正在尝试实现什么。 If you're trying to communicate between threads, I suggest using pipes.如果您尝试在线程之间进行通信,我建议使用管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM