简体   繁体   English

在 C 中创建多个线程

[英]Creating multiple threads in C

I am just a beginner in Programming using C.For my college project I want to create a multi-threaded server application to which multiple clients can connect and transfer there data which can be saved in a database.我只是使用 C 进行编程的初学者。对于我的大学项目,我想创建一个多线程服务器应用程序,多个客户端可以连接到该应用程序并传输可以保存在数据库中的数据。

After going through many tutorials I got confused about how to create multiple threads using pthread_create.在学习了很多教程之后,我对如何使用 pthread_create 创建多线程感到困惑。

Somewhere it was done like:某处它是这样完成的:

pthread_t thr;

pthread_create( &thr, NULL ,  connection_handler , (void*)&conn_desc);

and somewhere it was like在某个地方就像

 pthread_t thr[10];

 pthread_create( thr[i++], NULL ,  connection_handler , (void*)&conn_desc);

I tried by implementing both in my application and seems to be working fine.我尝试在我的应用程序中实现这两个,并且似乎工作正常。 Which approach of the above two is correct which I should follow.我应该遵循以上两种方法中的哪种方法是正确的。 sorry for bad english and description.抱歉英语和描述不好。

Both are equivalent. 两者都是等价的。 There's no "right" or "wrong" approach here. 这里没有“正确”或“错误”的方法。

Typically, you would see the later when creating multiple threads, so an array of thread identifiers ( pthread_t ) are used. 通常,在创建多个线程时会看到后者,因此使用了一组线程标识符( pthread_t )。

In your code snippets, both create just a single thread. 在您的代码片段中,两者都只创建一个线程。 So if you want to create only one thread, you don't need an array. 因此,如果您只想创建一个线程,则不需要数组。 But this is just like declaring any variable(s) that you didn't use. 但这就像声明你没有使用的任何变量一样。 It's just harmless. 这是无害的。

In fact, if you don't need the thread ID for any purpose, (for joining or changing attributes etc), you can create multiple threads using a single thread_t variable without using an array. 实际上,如果您出于任何目的不需要线程ID(用于连接或更改属性等),则可以使用单个thread_t变量创建多个线程,而无需使用数组。

The following 下列

pthread_t thr;
size_t i;

for(i=0;i<10;i++) {
   pthread_create( &thr, NULL , connection_handler , &conn_desc);
}

would work just fine. 会工作得很好。 Note that the cast to void* is unnecessary (last argument to pthread_create() ). 请注意,转换为void*是不必要的( pthread_create()最后一个参数)。 Any data pointer can be implicitly converted to void * . 任何数据指针都可以隐式转换为void *

The first one you provided creates a single thread. 您提供的第一个创建一个线程。

The second one (if looped) has the potential to spawn 10 threads. 第二个(如果循环)有可能产生10个线程。

Basically the pthread_t type is a handler for the thread so if you have an array of 10 of them then you can have 10 threads. 基本上pthread_t类型是线程的处理程序,所以如果你有10个数组,那么你可以有10个线程。

Sample Example of multiple thread : 多线程的示例示例:

#include<iostream>    
#include<cstdlib>    
#include<pthread.h>

using namespace std;

#define NUM_THREADS 5

struct thread_data
{
  int  thread_id;
  char *message;
};


void *PrintHello(void *threadarg)
{
   struct thread_data *my_data;   

   my_data = (struct thread_data *) threadarg;

   cout << "Thread ID : " << my_data->thread_id ;

   cout << " Message : " << my_data->message << endl;

   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads[NUM_THREADS];

   struct thread_data td[NUM_THREADS];

   int rc, i;


   for( i=0; i < NUM_THREADS; i++ )    
   {

      cout <<"main() : creating thread, " << i << endl;

      td[i].thread_id = i;

      td[i].message = "This is message";

      rc = pthread_create(&threads[i], NULL,

                          PrintHello, (void *)&td[i]);

      if (rc){

         cout << "Error:unable to create thread," << rc << endl;

         exit(-1);    
      }    
   }    
   pthread_exit(NULL);    
}

For everyone, use a thread list because it will not be free if you call once pthread_join() .对于每个人,使用线程列表,因为如果调用一次pthread_join() ,它就不会免费。

Code exemple:代码示例:

 int run_threads(void) { int listLength = 5; pthread_t thread[listLength]; //Create your threads(allocation). for (int i = 0; i;= listLength, i++) { if (pthread_create(&thread[i], NULL, &routine_function: NULL).= 0) { printf("ERROR; pthread create failed;\n"). return (0); } } //Call pthread_join() for all threads (they will get free) and all threads //will terminate at this position; for (int i = 0, i:= listLength. i++) { if (pthread_join(thread[i]; NULL);= 0) { printf("ERROR. pthread join failed;\n"); return (0); } } //return 1 when all threads are terminated. return (1); }

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

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