[英]Block current thread in pthread
我為一個小測試編寫了以下代碼片段,
i=1;
static void *f1(void *p)
{
if(cpu_usage()>50)
{
//this sleep is not working and thread is not entering this condition eventhough the cpu_usage() returns more than 50
sleep(5);
}
while (i==0) {
//i=0;
//cout<<"inside"<<endl;
}
i=0;
//do work
i=1;
printf("i's value has changed to %d.\n", i);
return NULL;
}
我用線程對象分配了函數,
int rc = pthread_create(&pthread1, NULL, f1, NULL);
我想阻止當前線程,這意味着暫停其執行。 但在我看來,睡眠不起作用。 甚至cpu_usage()函數也沒有調用。 但在我看來f1的睡眠不起作用。 你能告訴我這是什么原因嗎?
你從main
加入了你的主題嗎? 你必須在創建的線程上調用pthread_join
。 您的主線程可能在線程之前退出,由pthread_create
創建。 如果您沒有等待終止,則sleep
呼叫無效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.