Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- QT
- Linux
- Python
- management
- leadership
- Kuala Lumpur
- history
- psychology
- Software Engineering
- Programming
- France
- MySQL
- essay
- comic agile
- agile
- programming_book
- Book review
- Italy
- Book
- hbase
- erlang
- Malaysia
- Java
- hadoop
- Spain
- web
- program
- ubuntu
- django
- RFID
Archives
- Today
- Total
C에서 C++ 코드 호출 본문
출처: http://yesarang.tistory.com/69
// thread.c #include <stdio.h> #include <pthread.h> #include <unistd.h> // for sleep() #include <stdlib.h> // for free() void* start_routine(void* data) { int i = 0; printf("starts to run\n"); while ( ++i < 20 ) { printf("%d\n", i); sleep(1); } if ( data != NULL ) free(data); return NULL; } int main(void) { pthread_t thrd; int nr = 0; void* data; // created thread executes start_routine nr = pthread_create(&thrd, NULL, start_routine, NULL); // program will be splitted to 2 threads nr = pthread_join(thrd, &data); if ( data != NULL ) free(data); return 0; } /* $ gcc -o thread thread.c -lpthread $ ./thread starts to run 1 2 ... 18 19 */ // c2cpp.h #ifndef _C2CPP_H #define _C2CPP_H #include <iostream> #include <unistd.h> using namespace std; class UsefulCppClass { public: UsefulCppClass(string name): _name(name) {} void DoSomeAction() { cout << _name << ": I'm doing some useful job. step #1" << endl; sleep(1); cout << _name << ": I'm doing some useful job. step #2" << endl; sleep(1); cout << _name << ": I'm doing some useful job. step #3" << endl; sleep(1); cout << _name << ": I'm doing some useful job. step #4" << endl; sleep(1); cout << _name << ": I'm doing some useful job. step #5" << endl; sleep(1); cout << _name << ": Exiting..." << endl; sleep(1); } private: string _name; }; #endif // _C2CPP_H // c2cpp.cpp #include "c2cpp.h" int main() { pthread_t thrd; int nr; UsefulCppClass* pInst = new UsefulCppClass("01"); void* pData; nr = pthread_create(&thrd, NULL, &UsefulCppClass::DoSomeAction, NULL); pthread_join(thrd, &pData); return 0; } /* $ g++ -o c2cpp c2cpp.cpp -lpthread In file included from c2cpp.cpp:1: c2cpp.cpp: In function ‘int main()’: c2cpp.cpp:10: error: cannot convert ‘void (UsefulCppClass::*)()’ to ‘void* (*)(void*)’ for argument ‘3’ to ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’ */ // c2cpp.h // same as above // c2cpp.cpp #include "c2cpp.h" void* UsefulCppClassWrappingFunction(void* pInst) { cout << "UsefulCppClassWrappingFunction() called" << endl; // type casting to UsefulCppClass instance UsefulCppClass* pInstance = static_cast<UsefulCppClass*>(pInst); // call method pInstance->DoSomeAction(); } int main() { pthread_t thrd; int nr; UsefulCppClass* pInst = new UsefulCppClass("01"); void* pData; nr = pthread_create(&thrd, NULL, &UsefulCppClassWrappingFunction, NULL); pthread_join(thrd, &pData); return 0; } /* $ g++ -o c2cpp c2cpp.cpp -lpthread -g $ ./c2cpp UsefulCppClassWrappingFunction() called 01: I'm doing some useful job. step #1 01: I'm doing some useful job. step #2 01: I'm doing some useful job. step #3 01: I'm doing some useful job. step #4 01: I'm doing some useful job. step #5 01: Exiting... */ // c2cpp.h // same as above // c2cpp.cpp #include "c2cpp.h" class UsefulCppClassWrapper { public: static void* DoIt(void* pInst) { cout << "UsefulCppClassWrapper::DoIt() called" << endl; UsefulCppClass* pInstance = static_cast<UsefulCppClass*>(pInst); pInstance->DoSomeAction(); return NULL; } }; int main() { pthread_t thrd; int nr; UsefulCppClass* pInst = new UsefulCppClass("01"); void* pData; nr = pthread_create(&thrd, NULL, &UsefulCppClassWrapper::DoIt, pInst); pthread_join(thrd, &pData); return 0; }
Comments