我已经开始尝试使用ios平台的POSIX线程。使用NSThread来回复它是相当艰巨的。POSIX线程在ios上
基本上在我的示例应用程序中,我有一个大数组填充类型mystruct。每隔一段时间(非常频繁),我都想执行一个任务,其中一个结构的内容在的背景中,所以我将它传递给detachnewthread以解决问题。
我认为我有基础知识,但我想在尝试转向更复杂的东西之前获得专业意见。 我在这里有什么似乎“o.k”,你能指出任何可能导致问题的缺失吗?你可以发现任何内存管理问题等...
struct mystruct
{
pthread thread;
int a;
long c;
}
void detachnewthread(mystruct *str)
{
// pthread_t thread;
if(str)
{
int rc;
// printf("In detachnewthread: creating thread %d\n", str->soundid);
rc = pthread_create(&str->thread, NULL, DoStuffWithMyStruct, (void *)str);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
//exit(-1);
}
}
//
/* Last thing that main() should do */
// pthread_exit(NULL);
}
void *DoStuffWithMyStruct(void *threadid)
{
mystruct *sptr;
dptr = (mystruct *)threadid;
// do stuff with data in my struct
pthread_detach(soundptr->thread);
}
以防止被修改或从内存中删除的安全性我想我们会说互斥对吗? – dubbeat
@dubbeat:我添加了一些信息来试图回答这个问题。如果您提供更多关于子线程如何使用结构内容的细节,我(或其他人)可能会给出更详细的答案。 –