You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pegasus.apache.org by GitBox <gi...@apache.org> on 2020/10/26 09:43:59 UTC

[GitHub] [incubator-pegasus] Smityz opened a new issue #630: Add a util to manage info synchronize in mutithreads

Smityz opened a new issue #630:
URL: https://github.com/apache/incubator-pegasus/issues/630


   Reference to my [StackOverflowow question](https://stackoverflow.com/questions/64531392/is-it-a-good-way-to-keep-thread-safety-to-synchronize-the-object)
   I think we can add util to solve some synchronize problem in a lock-free way.
   Pseudocode:
   ```
   #include <bits/stdc++.h>
   #include <iostream>
   #include <utility>
   #include <thread>
   #include <chrono>
   #include <functional>
   #include <atomic>
   #include <unistd.h>
   #include <shared_ptr_atomic>
   
   using namespace std;
   
   #define ATOMIC_READLOCK(stored_data_ptr) \
       {                                    \
           auto atomic_readlock_##name = stored_data_ptr;
   
   #define ATOMIC_READLOCK_RELEASE() \
       }
   
   #define ATOMIC_WRITELOCK(stored_data_ptr, new_data_ptr) \
       stored_data_ptr = new_data_ptr        \
   
   class bar
   {
   public:
       bar():num(1) {}
       explicit bar(int x):num(x) {}
       int get_num()
       {
           return num;
       }
   private:
       int num;
   };
   
   vector <int> v;
   
   shared_ptr<bar> ptr_store;
   
   void get_func()
   {
       for (int i=0; i<100000; i++)
       {
           v.push_back(ptr_store->get_num());
           usleep(1);
       }
   }
   
   void set_func()
   {
       for (int i=0; i<100000; i++){
           atomic_store_explicit(ptr_store,make_shared<bar>(i));
       }
   }
   
   int main()
   {
       ptr_store = make_shared<bar>();
       std::thread t1(get_func);
       std::thread t2(set_func);
       t1.join();
       t2.join();
   }
   ```
   But here is a core dump, I will try to fix it.
   ```shell
   [Thread debugging using libthread_db enabled]
   Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
   Core was generated by `./reset'.
   Program terminated with signal SIGSEGV, Segmentation fault.
   #0  0x0000555a2a075c58 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() ()
   [Current thread is 1 (Thread 0x7f70244ec700 (LWP 30265))]
   (gdb) where
   #0  0x0000555a2a075c58 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() ()
   #1  0x0000555a2a0758b9 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() ()
   #2  0x0000555a2a075790 in std::__shared_ptr<bar, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() ()
   #3  0x0000555a2a0757c2 in std::shared_ptr<bar>::~shared_ptr() ()
   #4  0x0000555a2a0753cc in get_func() ()
   #5  0x0000555a2a076069 in void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) ()
   #6  0x0000555a2a075b3e in std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) ()
   #7  0x0000555a2a077b52 in decltype (__invoke((_S_declval<0ul>)())) std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) ()
   #8  0x0000555a2a077af0 in std::thread::_Invoker<std::tuple<void (*)()> >::operator()() ()
   #9  0x0000555a2a077a3c in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() ()
   #10 0x00007f70251706df in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
   #11 0x00007f7024c836db in start_thread (arg=0x7f70244ec700) at pthread_create.c:463
   #12 0x00007f70249aca3f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] Smityz commented on issue #630: Add a util to manage info synchronize in mutithreads

Posted by GitBox <gi...@apache.org>.
Smityz commented on issue #630:
URL: https://github.com/apache/incubator-pegasus/issues/630#issuecomment-717077582


   It will make some mistakes, forget it


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] Smityz closed issue #630: Add a util to manage info synchronize in mutithreads

Posted by GitBox <gi...@apache.org>.
Smityz closed issue #630:
URL: https://github.com/apache/incubator-pegasus/issues/630


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org