MCPcopy Create free account
hub / github.com/Tencent/MMKV / threadTest

Function threadTest

POSIX/demo/TestInterProcessLock.cpp:61–101  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59}
60
61bool threadTest() {
62 sem_t *semParent = sem_open("/mmkv_main", O_CREAT, 0644, 0);
63
64 //auto mmkv = MMKV::mmkvWithID(MMKV_ID, MMKV_MULTI_PROCESS);
65 auto fd = open("/tmp/mmkv/TestInterProcessLock.file", O_RDWR | O_CREAT | O_CLOEXEC, S_IRWXU);
66 flockPtr = new FileLock(fd);
67
68 sem_post(semParent);
69 cout << "Waiting for parent..." << endl;
70 usleep(1000);
71 sem_wait(semParent);
72 sem_close(semParent);
73 cout << "Parent locked" << endl;
74
75 semEnded = sem_open(MMKV_ID, O_CREAT, 0644, 0);
76
77 sem_t *semStarted = sem_open("/mmkv_sem_started", O_CREAT, 0644, 0);
78 if (!semStarted) {
79 printf("fail to create semphare: %d(%s)\n", errno, strerror(errno));
80 exit(1);
81 }
82 for (int index = 0; index < 2; ++index) {
83 pthread_t threadHandle;
84 pthread_create(&threadHandle, nullptr, threadFunction, semStarted);
85 sem_wait(semStarted);
86 threadIndex++;
87 }
88 sem_close(semStarted);
89
90 cout << "Waiting for any child exit..." << endl;
91 usleep(1000);
92 auto ret = sem_trywait(semEnded);
93 sem_close(semEnded);
94 cout << "Any child exit: " << (ret == 0) << endl;
95
96 delete flockPtr;
97 flockPtr = nullptr;
98 //close(fd);
99
100 return (ret != 0);
101}
102
103int main() {
104 locale::global(locale(""));

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected