我已經開始嘗試使用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:我添加了一些信息來試圖回答這個問題。如果您提供更多關於子線程如何使用結構內容的細節,我(或其他人)可能會給出更詳細的答案。 –