gpt4 book ai didi

c++ - 为什么我得到 "error: ‘pthread_delay_np’ was not declared in this scope”?

转载 作者:太空狗 更新时间:2023-10-29 11:30:30 25 4
gpt4 key购买 nike

我试图看看是否有可能避免线程在 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 之后立即启动?与调度程序有关吗?

提前致谢!

最佳答案

引自some mailing list :

It is also available on HP-UX, VMS and Tru64 UNIX. pthread_delay_np() originated in the D4 draft of the POSIX.1c standard. The main implementer of POSIX.1c D4 was OSF for Distributed Computing Environment (DCE). The suffix _np denotes that the API is non-portable and cannot be relied on to be available on another platform. Generally nanosleep() is what you want to use as a replacement.

关于c++ - 为什么我得到 "error: ‘pthread_delay_np’ was not declared in this scope”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4305294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com