0
在調用pthread_create()
之前,我創建了一個函數來設置pthread
的屬性。但是,pthread_create()
由於返回非零值而失敗。我不確定我的函數設置的屬性有什麼問題。我正在編寫g++
我的程序Ubuntu 16.04 LTS
。pthreads_create()返回非零值?
編譯和執行我使用:
g++ example.cpp -lpthread
./a.out
-
typedef void *(*DART_TASK_T)(void *);
typedef void (*DART_CALLBACK_T)(int);
static struct taskcontrolblock_s sysTask[MAX_DARTOS_THREADS];
static unsigned max_task_id_registered = 0;
static std :: bitset<sizeInBytes(MAX_DARTOS_THREADS)> taskIDs;
struct taskcontrolblock_s {
const char *name;
DART_TASK_T routine;
DART_CALLBACK_T cleanup_callback;
void *arg;
unsigned arg_len;
union {
uint8_t flags;
struct {
//uint8_t created:1;
//uint8_t running:1;
//uint8_t blocked:1;
//uint8_t suspended:1;
//uint8_t killed:1;
//uint8_t completed:1;
//uint8_t;
};
};
int priority;
pthread_t thread;
}__attribute__((packed));
static int dispatch_task(int tid)
{
if (!taskIDs[tid]) //
return -1;
pthread_attr_t attr;
struct sched_param param;
param.sched_priority = sysTask[tid].priority;
int policy = SCHED_RR;
if (pthread_attr_init(&attr))
return -1;
if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED))
return -1;
if (pthread_attr_setschedpolicy(&attr, policy))
return -1;
if (pthread_attr_setschedparam(&attr, ¶m))
return -1;
if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM))
return -1;
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
return -1;
if (pthread_create(&sysTask[tid].thread, &attr, sysTask[tid].routine, sysTask[tid].arg))
return -1;
pthread_attr_destroy(&attr);
return 0;
}
編輯:
調用perror()
打印的字符數組Operation not permitted
到stderr
。
以root權限運行程序解決了問題。
您將「struct sched_param param」的某些成員未初始化。 –
使用perror()來顯示失敗的原因,這將有助於我們理解最新的錯誤。 –
請注意,只有root用戶纔可以將調度程序設置爲SCHED_RR – nos