2013-02-04 2 views
0

매우 간단한 크로스 플랫폼 (Linux/Windows) 스레드 기능을 만드는 방법을 알고 있습니다.다중 플랫폼 목적을위한 Clone C++ 데이터 유형

#if LINUX 
#include <pthread.h> 
ThreadHandle createThread(???* callback, void* data) { //I dont know what is data type of function pointer, sorry 
    pthread_t handle; 
    pthread_create(&handle, 0, callback, (void*)data); 
    return (ThreadHandle)handle; 
} 
define_data_type ThreadHandle = pthread_t; //I don't know how this is done at all, sorry 
#endif 
#if WINDOWS 
    #include <windows.h> 
    ThreadHandle createThread(???* callback, void* data) { 
     HANDLE handle = CreateThread( 
     NULL,     // default security attributes 
     0,      // use default stack size 
     callback,    // thread function name 
     data,     // argument to thread function 
     0,      // use default creation flags 
     NULL); // returns the thread identifier - I don't need this, do I? 
    } 
    define_data_type ThreadHandle = HANDLE; //I don't know how this is done at all, sorry 
#endif 

나는이 처음 veird 질문처럼 보이지만, 나는 초보자있어 마음에 계속됩니다 두려워, 나는 C를 이해할 필요 ++ : 이것은 내 샘플 코드입니다. 언제든지 을 입력하십시오. "모르겠다"는 부분이입니다.
이것이 잘못된 질문이라고 생각되면 어떻게해야하는지에 대한 의견을 남겨주세요. 모든 스레드 기능

  • 가에서 *. $의 platform.cpp 파일을 플랫폼 특정 코드를 추상적 인 것 Thread.h 같은 플랫폼 독립적 인 헤더를 가짐으로써

  • +4

    모든 환경에서 ['std :: thread'] (http://en.cppreference.com/w/cpp/thread)가 작동합니까? –

    +0

    와우, 그 std :: 모든 치료법처럼 보입니다. 매번 다중 플랫폼을 찾으면 누군가 std :: function으로 나타납니다. 고맙습니다. 그러나 나는 아직도이 크로스 플랫폼 물건을하는 방법에 관심이 있어요. –

    +1

    궁금하거나 환경이 아직 C++ 11이 아닌 경우 Boost에서 많은 다른 OS로 포팅 된 쓰레드 래퍼 클래스를 볼 수있다. –

    답변

    1
    1. 시작 분명히 빌드 시스템은 플랫폼을 컴파일해야
    2. 특정 코드에 대한 지금 관련 코드

    이 같은

    사용 무언가가 일반적인 유형

    ,369을 정의

    typedef unsigned long os_error_t; 당신이

    같은 것을 사용할 수있는 콜백 들어 특정 IMPL

    에 대한 문서를 읽어 리눅스에

    typedef void * os_console_handle; 
    typedef void * os_thread_handle; 
    typedef void * os_event_handle; 
    typedef unsigned long os_pid_t; 
    typedef unsigned long os_thread_id; 
    

    , 사용 (..) 창 사용에 을는 pthread_create를 호출하고 CreateThread (..) 전화

    typedef os_error_t (*thread_start_function_t)(void *); 
    
    class InternalThreadArgs { 
        thread_start_function_t m_func; 
        void *m_pArg; 
    public: 
        InternalThreadArgs(thread_start_function_t pf, void *pArg) { 
         m_func = pf; 
         m_pArg = pArg; 
        } 
        os_error_t Run() { 
         return m_func(m_pArg); 
        } 
    }; 
    

    지금, 는

     os_error_t create_thread(thread_start_function_t pf, void *pArg, os_thread_handle *pHandle); 
    
    처럼 추상 메소드의 서명이
    +0

    'typedef'가 내가 찾고있는 것 같아요. 우리에게 좋은 점을 여러 개 사용하면 더 많은 멀티 플랫폼 기능이 필요할 것입니다. –