我想创建n个线程.然后传递一个结构,每个结构用数据填充该结构; 例如bool来跟踪线程是否已完成或是否已终止信号.
n = 5; // For testing.
pthread_t threads[n];
for(i=0; i<n; i++)
pthread_create(&threads[i], &thread_structs[i], &functionX);
Run Code Online (Sandbox Code Playgroud)
假设thread_structs已被malloced.
functionX()通知功能内部没有参数.我应该为结构创建一个参数吗?或者我传递结构的地方还可以吗?
我如何指向刚刚传递给函数的结构?
那不是你使用pthread_create的方式:
http://man7.org/linux/man-pages/man3/pthread_create.3.html
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
Run Code Online (Sandbox Code Playgroud)
第三个参数是您的例程,第四个参数将转发到您的例程.你的日常应该是这样的:
void* functionX(void* voidArg)
{
thread_struct* arg = (thread_struct*)voidArg;
...
Run Code Online (Sandbox Code Playgroud)
并且pthread调用应该是:
pthread_create(&threads[i], NULL, functionX, &thread_structs[i]);
Run Code Online (Sandbox Code Playgroud)
(除非你有一个pthread_attr_t作为第二个参数提供).
| 归档时间: |
|
| 查看次数: |
183 次 |
| 最近记录: |