2017-07-24 61 views
1

已知 - 線程的線程ID。
要求 - 設置線程ID的Linux優先級。
約束 - 不能使用setpriority()可
替代setpriority(PRIO_PROCESS,thread_id,priority)

我曾嘗試使用以下

pthread_setschedprio(的pthread_t螺紋,INT PRIO);
pthread_setschedparam(pthread_t thread,int policy, const struct sched_pa​​ram * param);

上述兩個API都使用pthread_t作爲參數。我無法從線程ID構造(類型轉換)pthread_t。我知道轉換這是不可能的,由於不同的類型。

有沒有辦法仍然可以做到這一點?

+0

從哪裏得到線程ID? –

回答

1

pthread_setschedprio接口的某些方面可用於帶sched_setparam函數(在<thread.h>中聲明)的普通線程ID。 sched_setparam manual page表示該進程受到影響(這是POSIX規定的行爲),但在Linux上,it's actually the thread of that ID

請記住,直接調用sched_setparam可能會破壞PI互斥鎖和其他同步基元的預期行爲,因爲直接調用不會執行由pthread_ *函數執行的附加簿記。

+0

謝謝。它按預期工作。在Google上搜尋時錯過了API。 – user2618142