fork download
  1. #include <thread>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. volatile bool flag = false;
  6. class ctest{
  7. size_t cnt;
  8. public:
  9. ctest(size_t cnt = 0){
  10. this->cnt = cnt;
  11. }
  12. void inc(){
  13. cnt++;
  14. }
  15. void dec(){
  16. cnt--;
  17. }
  18. size_t count(){
  19. return cnt;
  20. }
  21. static void thread_proc(void * param){
  22. ctest * pthis = (ctest *)param;
  23. if( pthis != 0 )
  24. {
  25. pthis->inc();
  26. flag = false;
  27. }
  28. }
  29. };
  30.  
  31. int main(){
  32. ctest p;
  33. thread t(ctest::thread_proc, &p);
  34. flag = true;
  35. t.detach();
  36. while( flag ){
  37. //cout<<"\rwait"<<endl;
  38. }
  39. cout<<p.count()<<endl;
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 11656KB
stdin
Standard input is empty
stdout
1