1
塊當前線程我寫了下面的代碼片斷爲一個小的測試,在並行線程
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的睡眠不起作用。你們能告訴我是什麼原因嗎?
你爲什麼覺得'sleep()'不工作?你有計時嗎? – trojanfoe 2012-03-12 10:56:36
,因爲如果我省略了睡眠(1),它需要相同的時間。 – 2012-03-12 10:57:54
內核調度可能是一個問題。將時間代碼放入線程代碼中,並將開始和結束時間打印到'stdout'。 – trojanfoe 2012-03-12 10:59:06