C语言 C、如何使用pthread_create函数创建线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6990888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C , how to create thread using pthread_create function
提问by Leanne
I'm making a c file for a dispatch queue that gets a task and put it in to a queue which is the linked list. In order to do this, I need to create threads using
我正在为调度队列制作 ac 文件,该队列获取任务并将其放入链表队列中。为了做到这一点,我需要使用创建线程
pthread_t cThread;
if(pthread_create(&cThread, NULL, work, param)){
perror("ERROR creating thread.");
}
However I need to make another function that goes into 'work' and 'param' variable as parameters of create function. My friend told me that I just need to put any code in the work function that loops infinitely so the thread does not die.. Can anyone explain each parameter goes in to the pthread_createfunction- especially for workand param? I searched Google for this, but most of tutorials are so hard to understand the concept...
但是,我需要制作另一个进入“工作”和“参数”变量的函数作为创建函数的参数。我的朋友告诉我,我只需要在无限循环的工作函数中放置任何代码,这样线程就不会死.. 谁能解释每个参数进入pthread_create函数 - 特别是对于workand param?我在谷歌上搜索过这个,但大多数教程都很难理解这个概念......
回答by duskwuff -inactive-
The four parameters to pthread_createare, in order:
四个参数pthread_create依次为:
A pointer to a
pthread_tstructure, whichpthread_createwill fill out with information on the thread it creates.A pointer to a
pthread_attr_twith parameters for the thread. You can safely just passNULLmost of the time.A function to run in the thread. The function must return
void *and take avoid *argument, which you may use however you see fit. (For instance, if you're starting multiple threads with the same function, you can use this parameter to distinguish them.)The
void *that you want to start up the thread with. PassNULLif you don't need it.
指向
pthread_t结构的指针,该结构pthread_create将填充有关它创建的线程的信息。指向
pthread_attr_t带参数的线程的指针。NULL大多数时候你可以安全地通过。在线程中运行的函数。该函数必须返回
void *并接受一个void *参数,您可以随意使用该参数。(例如,如果您正在启动具有相同功能的多个线程,则可以使用此参数来区分它们。)将
void *要启动与线程。NULL不需要就通过吧。
回答by Foo Bah
clarifying duskwuff's answer:
澄清 duskwuff 的回答:
workparameter is a function pointer. The function should take one argument which is indicated as type void *and return value void *.
work参数是一个函数指针。该函数应采用一个参数,该参数表示为 typevoid *和 return value void *。
paramis expected to be a pointer to the data that workwill receive.
param预期是指向work将接收的数据的指针。
As an example, lets say you want to pass two int to the worker. Then, you can create something like this:
例如,假设您想将两个 int 传递给工作人员。然后,您可以创建如下内容:
int *param = (int *)malloc(2 * sizeof(int));
param[0] = 123;
param[1] = 456;
pthread_create(&cThread, NULL, work, param);
Then your work function can convert the pointer type and grab the param data:
然后您的工作函数可以转换指针类型并获取参数数据:
void *work(void * parm) {
int *param = (int *)parm;
int first_val = param[0];
....
}
You can do more complex stuff, like creating a struct with all of the data you need to pass.
你可以做更复杂的事情,比如用你需要传递的所有数据创建一个结构。

