刚刚遇到一个关于如何将第四个参数传递给 pthread_create() 的奇怪问题。
原来,我写的代码如下:
auditLogEntry *newEntry = NULL;
// malloc and init the memory for newEntry
rc = audit_init_log_entry(&newEntry);
// wrapper of 'goto cleanup'
ERR_IF( rc != 0 );
...
rc2 = pthread_attr_init(&attr);
ERR_IF( rc2 != 0 );
rc2 = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ERR_IF( rc2 != 0 );
rc2 = pthread_create(&syslog_thread, &attr, syslog_thread_handler, (void *)newEntry);
ERR_IF( rc2 != 0 );
newEntry = NULL;
...
cleanup:
pthread_attr_destroy(&attr);
if (newEntry != NULL)
{
audit_free_log_entry(newEntry);
newEntry = NULL;
}
static void *syslog_thread_handler(void *t)
{
auditLogEntry *entry = (auditLogEntry *)t;
... // code using entry
cleanup:
audit_free_log_entry(entry);
pthread_exit(0);
}
一切正常。
然后,我做了一个改变:
rc2 = pthread_create(&syslog_thread, &attr, syslog_thread_handler, (void *)&newEntry);
ERR_IF( rc2 != 0 );
...
cleanup:
pthread_attr_destroy(&attr);
if (rc != 0 && newEntry != NULL)
{
audit_free_log_entry(newEntry);
newEntry = NULL;
}
static void *syslog_thread_handler(void *t)
{
auditLogEntry **entry = (auditLogEntry **)t;
... // code using *entry
cleanup:
audit_free_log_entry(*entry);
*entry = NULL;
pthread_exit(0);
}
在上述更改之后,线程处理程序将使用 *entry 来访问日志条目数据。但它没有用。更糟糕的是,进程核心被转储了。
我尝试了“man pthread_create”,但没有特别提到应该如何将最后一个参数传递给它。
我这里有什么错吗?