![](/img/trans.png)
[英]why compiler says ‘pthread_getthreadid_np’ was not declared in this scope?
[英]Why do I get “error: ‘pthread_delay_np’ was not declared in this scope”?
我试图查看是否有可能避免thread_create之后立即启动线程。 因此,我遇到了pthread_delay_np并尝试了该示例:
#define _MULTI_THREADED
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#define NTHREADS 5
void *threadfunc(void *parm)
{
int rc;
struct timespec ts = {0, 0};
/* 5 and 1/2 seconds */
ts.tv_sec = 5;
ts.tv_nsec = 500000000;
printf("Thread blocked\n");
rc = pthread_delay_np(&ts);
if (rc != 0) {
printf("pthread_delay_np() - return code %d\n", rc);
return (void*)&rc;
}
printf("Wait timed out!\n");
return NULL;
}
int main(int argc, char **argv)
{
int rc=0;
int i;
pthread_t threadid[NTHREADS];
void *status;
int fail=0;
printf("Enter Testcase - %s\n", argv[0]);
printf("Create %d threads\n", NTHREADS);
for(i=0; i<NTHREADS; ++i) {
rc = pthread_create(&threadid[i], NULL, threadfunc, NULL);
}
printf("Wait for threads and cleanup\n");
for (i=0; i<NTHREADS; ++i) {
rc = pthread_join(threadid[i], &status);
if (status != NULL) {
fail = 1;
}
}
if (fail) {
printf("At least one thread failed!\n");
return 1;
}
printf("Main completed\n");
return 0;
}
但是我不断收到“错误:在此范围内未声明'pthread_delay_np'”。 有人知道为什么吗?
另外,还有其他方法可以防止线程在thread_create之后立即启动吗? 与调度程序有关系吗?
先感谢您!!
从一些邮件列表中报价:
它在HP-UX,VMS和Tru64 UNIX上也可用。
pthread_delay_np()
起源于POSIX.1c标准的D4草案。 POSIX.1c D4的主要实现者是用于分布式计算环境(DCE)的OSF。 后缀_np表示该API是不可移植的,不能依赖于在其他平台上可用。 通常,nanosleep()
是您要用作替代品的东西。
只需使用nanosleep(2)
。
看来pthread_delay_np
是非标准的,不可移植的(因此带有“ _np”后缀)功能。 您确定平台中存在它吗?
您可以使用信号量向线程指示它可以在信号量等待点之后开始执行。
答案可能取决于您通过避免立即启动线程来尝试实现的目标。
编辑
但是,我非常确定固定延迟不是您真正需要的,除非您正在与环境(例如硬件)进行交互或只是进行实验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.