对线程的C11支持很好,但不是很好。如果需要使用pthread函数,我想了解将thrd_t
强制转换为pthread_t
是否合法。例如:
#include <threads.h>
#define_GNU_SOURCE
#include <pthread.h>
#include <sched.h>
int main(void) {
thrd_t t;
cpu_set_t cpuset;
// some initialization code here...
pthread_setaffinity_np((pthread_t)t,sizeof(cpuset),&cpuset) // Is the cast valid?
// other code here...
}
C ++ 11提供了std::thread::native_handle来获取pthread_t
的值在pthread函数中,但是C11没有提供这样的函数。由于thrd_t
和pthread_t
都是unsigned long int
的typedef,因此我想它们是兼容的。标准怎么说呢?
编辑:同一问题也适用于threads.h
提供的其他两种类型,即mtx_t
和cnd_t
。