我正试图看到,能否避免一线read子在read子之后立即开始。 因此,我走过了read子,我尝试了:
#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
");
rc = pthread_delay_np(&ts);
if (rc != 0) {
printf("pthread_delay_np() - return code %d
", rc);
return (void*)&rc;
}
printf("Wait timed out!
");
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
", argv[0]);
printf("Create %d threads
", NTHREADS);
for(i=0; i<NTHREADS; ++i) {
rc = pthread_create(&threadid[i], NULL, threadfunc, NULL);
}
printf("Wait for threads and cleanup
");
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!
");
return 1;
}
printf("Main completed
");
return 0;
}
但是,我保留了“error:`pthread_delay_np'这个范围没有宣布”。 没有人知道为什么?
此外,还没有任何其他办法防止透镜在read后立即开始? 是否与时间表有关?
advance!