int main() { Barrier barrier1(3); Barrier barrier2(3); arrived_all = false; processed_all = true; arrived_count.store(0); processed_count.store(allowed_max); std::thread worker[50]; for (int i = 0; i < 6; i++) worker[i] = std::thread(worker_thread, i, &barrier1, &barrier2); { std::lock_guard<std::mutex> lk(m); std::cout << "Back in main(), after initialing first thread batch " << data << std::endl; } // wait until worker dies finishes execution for (int i = 0; i < 6; i++) worker[i].join(); // for (int i = 5; i < 10; i++) // worker[i] = std::thread(worker_thread, i); // { // std::lock_guard<std::mutex> lk(m); // std::cout << "Back in main(), after initialing second thread batch." << std::endl; // } // // wait until worker dies finishes execution // for (int i = 5; i < 10; i++) // worker[i].join(); std::cout << "finished main(), data = " << data << std::endl; }
static void onSurfaceDestroyed(JNIEnv */*env*/, jobject /*object*/) { s_currentNativeWindowMutex.lock(); s_currentNativeWindow.reset(); s_currentNativeWindowId.store(0); s_currentNativeWindowMutex.unlock(); }
void spring_time::sleep(bool forceThreadSleep) { if (forceThreadSleep) { spring::this_thread::sleep_for(chrono::nanoseconds(toNanoSecsi())); return; } // for very short time intervals use a yielding loop (yield is ~5x more accurate than sleep(), check the UnitTest) if (toMicroSecsi() < (avgThreadSleepTimeMicroSecs + avgThreadYieldTimeMicroSecs * 5)) { const spring_time s = gettime(); while ((gettime() - s) < *this) thread_yield(); return; } // expected wakeup time const spring_time t0 = gettime() + *this; spring::this_thread::sleep_for(chrono::nanoseconds(toNanoSecsi())); const spring_time t1 = gettime(); const spring_time dt = t1 - t0; if (t1 >= t0) { // yes, it's not 100% thread correct, but it's okay when 1 of 1 million writes is dropped int avg = avgThreadSleepTimeMicroSecs.load(); int newAvg = mix<float>(avg, dt.toMicroSecsf(), 0.1f); avgThreadSleepTimeMicroSecs.store(newAvg); } }
static void thread_yield() { const spring_time t0 = spring_time::gettime(); this_thread::yield(); const spring_time t1 = spring_time::gettime(); const spring_time dt = t1 - t0; if (t1 >= t0) { // yes, it's not 100% thread correct, but it's okay when 1 of 1 million writes is dropped int avg = avgThreadYieldTimeMicroSecs.load(); int newAvg = mix<float>(avg, dt.toMicroSecsf(), 0.1f); avgThreadYieldTimeMicroSecs.store(newAvg); } }
void handle_subscribe_multiple (int argc, const char *argv[]) { uint32_t count = atoi(argv[1]); pthread_t *tid; CommandContext context(argc, argv, NULL); // Create N threads tid = new pthread_t[count]; for (int i = 0; i < count; i++) { pthread_create(&tid[i], NULL, proc, (void *) &context); } // Give few mins to subscribe all sleep(5); // Reset id some_id.store(0); // Wait for them to finish for (int i = 0; i < count; i++) { pthread_join(tid[i], NULL); } }
bool reader(){ { // creating one reader std::this_thread::sleep_for (std::chrono::milliseconds(10)); Reader r("/tmp/check-stress", [](void *data, size_t size){ // auto frame = static_cast<Frame *>(data); // std::cout << "(one reader) new data for client " // << frame->count // << " (size " << size << ")" // << std::endl; }, nullptr, nullptr, &logger); assert(r); std::cout << "one more reader" << std::endl; std::this_thread::sleep_for (std::chrono::milliseconds(1000)); } std::cout << "one less reader" << std::endl; { // creating five readers std::this_thread::sleep_for (std::chrono::milliseconds(10)); Reader r1("/tmp/check-stress", [](void *data, size_t size){ // auto frame = static_cast<Frame *>(data); // std::cout << "(1) new data for client " // << frame->count // << " (size " << size << ")" // << std::endl; }, nullptr, nullptr, &logger); assert(r1); Reader r2("/tmp/check-stress", [](void *data, size_t size){ // auto frame = static_cast<Frame *>(data); // std::cout << "(2) new data for client " // << frame->count // << " (size " << size << ")" // << std::endl; }, nullptr, nullptr, &logger); assert(r2); Reader r3("/tmp/check-stress", [](void *data, size_t size){ // auto frame = static_cast<Frame *>(data); // std::cout << "(3) new data for client " // << frame->count // << " (size " << size << ")" // << std::endl; }, nullptr, nullptr, &logger); assert(r3); Reader r4("/tmp/check-stress", [](void *data, size_t size){ // auto frame = static_cast<Frame *>(data); // std::cout << "(4) new data for client " // << frame->count // << " (size " << size << ")" // << std::endl; }, nullptr, nullptr, &logger); assert(r4); Reader r5("/tmp/check-stress", [](void *data, size_t size){ // auto frame = static_cast<Frame *>(data); // std::cout << "(5) new data for client " // << frame->count // << " (size " << size << ")" // << std::endl; }, nullptr, nullptr, &logger); assert(r5); std::cout << "five more reader" << std::endl; std::this_thread::sleep_for (std::chrono::milliseconds(1000)); } std::this_thread::sleep_for (std::chrono::milliseconds(100)); done.store(1); std::cout << "done set to 1" << std::endl; return true; }
int main(int argc, char **argv) { int i; std::ostringstream conffile; conffile << SYSCONFDIR << "/restfsd.ini"; Config conf(conffile.str()); const int THREAD_COUNT = conf.GetInteger("main","threads",2); std::thread *id = new std::thread[THREAD_COUNT]; FCGX_Init(); socketId.store( FCGX_OpenSocket(conf.get("main","listen").c_str(), 20) ); if(socketId < 0) { printf("Socket isn't opened\n"); return 1; } //~ int pid; //~ pid = fork(); //~ if (pid == -1) //~ { //~ printf("Error: Start Daemon failed (%s)\n", strerror(errno)); //~ return -1; //~ } //~ else if (!pid) //~ { //~ umask(0); //~ setsid(); //~ //chdir("/home/sokol"); for(i = 0; i < THREAD_COUNT; i++) { id[i] = std::thread( doit, i, std::ref(conf) ); } //~ sleep(1); //~ std::cout << "main1 " << conf.get("test","test") << std::endl; //~ for(i = 0; i < THREAD_COUNT; i++) //~ { //~ id[i].join(); //~ id[i].detach(); //~ } //~ while(true) { //~ //std::cout << "while" << std::endl; //~ for(i = 0; i < THREAD_COUNT; i++) //~ { //~ id[i].join(); //~ std::cout << "thread[" << i << "].get_id: " << id[i].get_id() << std::endl; //~ if (id[i].joinable()) //~ { //~ id[i].join(); //~ } else //~ { //~ std::cout << "thread[" << i << "]: starting" << std::endl; //~ id[i] = std::thread( doit, i, std::ref(conf) ); //~ } //~ } //~ //sleep(1); //~ } //~ close(STDIN_FILENO); //~ close(STDOUT_FILENO); //~ close(STDERR_FILENO); for(i = 0; i < THREAD_COUNT; i++) { id[i].join(); } //~ std::cout << "main2" << conf.get("test","test") << std::endl; return 0; //~ } else //~ { //~ printf("Start Daemon pid=(%d)\n", pid); //~ return 0; //~ } }