【发布时间】:2010-11-29 15:40:28
【问题描述】:
我试图查看是否有可能避免线程在 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 之后立即启动吗?跟调度器有什么关系?
提前谢谢你!!
【问题讨论】: