Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- //------------------------------------------------------------------------------
- class IFactory
- {
- public:
- virtual ~IFactory() { /* */ }
- virtual void bar() { printf("Default bar\n"); };
- private:
- };
- class Factory : public IFactory
- {
- public:
- virtual void bar() { printf("Factory::bar()\n"); }
- private:
- };
- //------------------------------------------------------------------------------
- IFactory * get_factory_instance()
- {
- static IFactory * s_factory_instance = NULL;
- if (s_factory_instance == NULL) {
- IFactory * factory = new Factory();
- if (__sync_val_compare_and_swap(&s_factory_instance, NULL, factory) != NULL) {
- delete factory;
- }
- }
- return s_factory_instance;
- }
- //------------------------------------------------------------------------------
- void * thread(void * x)
- {
- IFactory * impl_factory = get_factory_instance();
- impl_factory->bar();
- return NULL;
- }
- //------------------------------------------------------------------------------
- int main()
- {
- pthread_t t[2];
- pthread_create(&t[0], NULL, thread, NULL);
- pthread_create(&t[1], NULL, thread, NULL);
- pthread_join(t[0], NULL);
- pthread_join(t[1], NULL);
- return 0;
- }
- /*
- Factory::bar()
- ==================
- WARNING: ThreadSanitizer: data race on vptr (ctor/dtor vs virtual call) (pid=2452)
- Read of size 8 at 0x7d040000eff0 by thread T2:
- #0 thread(void*) /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:43:5 (test_vptr_con+0x00000009eb71)
- Previous write of size 8 at 0x7d040000eff0 by thread T1:
- #0 operator new(unsigned long) <null>:0 (test_vptr_con+0x0000000273fd)
- #1 get_factory_instance() /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:30:9 (test_vptr_con+0x00000009ea20)
- #2 thread(void*) /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:42:31 (test_vptr_con+0x00000009eb40)
- Location is heap block of size 8 at 0x7d040000eff0 allocated by thread T1:
- #0 operator new(unsigned long) <null>:0 (test_vptr_con+0x0000000273fd)
- #1 get_factory_instance() /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:30:9 (test_vptr_con+0x00000009ea20)
- #2 thread(void*) /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:42:31 (test_vptr_con+0x00000009eb40)
- Thread T2 (tid=2464, running) created by main thread at:
- #0 pthread_create <null>:0 (test_vptr_con+0x00000002a6c1)
- #1 main /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:53:3 (test_vptr_con+0x00000009ec13)
- Thread T1 (tid=2463, finished) created by main thread at:
- #0 pthread_create <null>:0 (test_vptr_con+0x00000002a6c1)
- #1 main /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:52:3 (test_vptr_con+0x00000009ebe9)
- SUMMARY: ThreadSanitizer: data race on vptr (ctor/dtor vs virtual call) /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:43 thread(void*)
- ==================
- Factory::bar()
- ThreadSanitizer: reported 1 warnings
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement