2010-11-29 4 views
2

thread_create 바로 다음에 스레드가 시작되는 것을 피할 수 있는지 확인하려고했습니다. 그래서, pthread_delay_np을 가로 질러 와서 나는 예를 시도 :"오류 : '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 직후에 스레드가 시작되지 않도록 다른 방법이 있습니까? 스케줄러와 관련이 있습니까?

미리 감사드립니다.

답변

1

pthread_delay_np은 비표준의 이식 가능하지 않은 (따라서 "_np"접미사) 기능입니다. 플랫폼에 해당하는지 확실합니까?

+0

음 ... 분명히 내 플랫폼에는 존재하지 않습니다. :(그럼, thread_create를 수행 한 직후부터 쓰레드가 시작되는 것을 막기 위해 무엇을 할 수 있습니까? – delay

+0

@delay : 다른 대답들이 말했듯이, 당신은 단지'nanosleep'을 사용할 수 있어야합니다. –

3

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.

1

세마포어를 사용하여 세마포어 대기 지점을지나 실행을 시작할 수 있음을 스레드에 알릴 수 있습니다.

대답은 아마도 스레드의 즉각적인 시작을 피함으로써 달성하려는 것에 달려 있습니다.

편집

는 그러나, 나는 고정 된 지연은 환경 (예를 들어, 하드웨어) 또는 단지 실험과 상호 작용하지 않는 한 당신이 실제로 필요 하지입니다 확신 해요.

관련 문제