int main() { boost::thread thrd1(&sender); boost::thread thrd2(&receiver); thrd1.join(); thrd2.join(); }
int main(int argc, char* argv[]) { boost::thread thrd1(count(1)); boost::thread thrd2(count(2)); thrd1.join(); thrd2.join(); return 0; }
void multithread_push_pop(int64_t counts) { std::atomic_bool start(false); std::atomic_bool quit(false); int64_t push_sum = 0; int64_t pop_sum1 = 0, pop_sum2 = 0; WSQ_t q; std:: thread thrd1([&start, counts, &push_sum, &pop_sum1, &q, &quit](){ while(!start) std::this_thread::yield(); std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); for(int64_t i =0; i < counts; ++i){ if(q.push_back(i)) push_sum += i; if(i % 3 == 0) { int64_t t; if(q.pop(t)) pop_sum1 += t; } } while(q.size() != 0){ int64_t t; if(q.pop(t)) pop_sum1 +=t; } quit = true; end = std::chrono::system_clock::now(); auto elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count(); g_print_mutex.lock(); std::cout<<"push/pop thread : "<<elapsed_seconds<<std::endl; g_print_mutex.unlock(); }); std::thread thrd2([&start, &pop_sum2, &q, & quit](){ while(!start) std::this_thread::yield(); std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); while(!quit) { int64_t t; if(q.steal(t)){ pop_sum2 += t; } } end = std::chrono::system_clock::now(); auto elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count(); g_print_mutex.lock(); std::cout<<"steal thread : "<<elapsed_seconds<<std::endl; g_print_mutex.unlock(); }); start = true; thrd1.join(); thrd2.join(); if(push_sum != pop_sum1 + pop_sum2) { std::cout<<"bugs here! push sum is: "<<push_sum<<", pop_sum is "<<pop_sum1 + pop_sum2<<std::endl; } }
bool startipc() { //flag that ipc is started in this thread tss_ipcstarted.reset(new bool(true)); //ensure thread's environment number is available or quit // if (!tss_environmentns.get()) // return false; //get thread's mv environment number int environmentn=getenvironmentn(); //start another thread (for this threads environment) to calculate any dictionary items required by the db server backend //ALN:DANGEROUS: following line works only if tss_pgconnparams was reset with some string boost::thread thrd1(boost::bind(&MVipc, environmentn, *tss_pgconnparams.get())); //wait for the new thread to signal that it is listening for requests before resuming boost::mutex::scoped_lock lock(global_ipcmutex); //TODO make sure notifies CORRECT thread by using array of ipcmutexes and environmentn //TODO put a timeout in case the pipe doesnt open #if TRACING > 3 std::wclog<<L"startipc() Waiting for pipe to be opened\n"; #endif global_ipccondition.wait(lock); #if TRACING > 3 std::wclog<<L"startipc() Waited for pipe to be opened\n"; #endif return true; }
int main(int argc, char* argv[]) { std::thread thrd1(std::bind(&count, 1)); std::thread thrd2(std::bind(&count, 2)); thrd1.join(); thrd2.join(); return 0; }
int main(int argc, char* argv[]) { boost::thread thrd1(&reader); boost::thread thrd2(&writer); thrd1.join(); thrd2.join(); return 0; }
int main(int, char*[]) { boost::thread thrd1(&sender); boost::thread thrd2(&receiver); thrd1.join(); thrd2.join(); return 0; }
void do_test(M* dummy=0) { typedef buffer_t<M> buffer_type; buffer_type::get_buffer(); boost::thread thrd1(&buffer_type::do_receiver_thread); boost::thread thrd2(&buffer_type::do_receiver_thread); boost::thread thrd3(&buffer_type::do_sender_thread); thrd1.join(); thrd2.join(); thrd3.join(); }
bool TCPSocket001::run() { class ClientSocket : public elw::IRunnable { std::string m_strRemoteAddr; word m_wRemotePort; bool m_fSuccess; public: ClientSocket(const std::string& strRemoteAddr, word wRemotePort) : m_strRemoteAddr(strRemoteAddr), m_wRemotePort(wRemotePort), m_fSuccess(true) { } protected: int run() { elw::Socket sock; if (!sock.create()) goto _ERROR; sock.connect(m_strRemoteAddr, m_wRemotePort); if (9 != sock.send("123456789", 9)) goto _ERROR; if (1 != sock.send("0", 1)) goto _ERROR; if (3 != sock.send("987", 3)) goto _ERROR; if (2 != sock.send("65", 2)) goto _ERROR; if (4 != sock.send("4321", 4)) goto _ERROR; sock.shutdown(elw::SockBase::rw); sock.close(); return 0; _ERROR: m_fSuccess = false; sock.shutdown(elw::SockBase::rw); sock.close(); return 0; } public: bool isSuccess() const { return m_fSuccess; } } clnt("127.0.0.1", 22000); class ServerSocket : public elw::IRunnable { word m_wLinteningPort; bool m_fSuccess; public: ServerSocket(word wListeningPort) : m_wLinteningPort(wListeningPort), m_fSuccess(true) { } protected: int run() { elw::SmartPtr<elw::Socket> ptr; elw::ServerSocket serv; char sz[32] = { 0 }; uint uTotalRead = 0; std::string str; if (!serv.create()) goto _ERROR; if (!serv.bind(m_wLinteningPort)) goto _ERROR; ptr = serv.accept(); for ( ; ; ) { int iRead = ptr->recv(sz + uTotalRead, sizeof(sz) - uTotalRead, elw::SockBase::none); if (iRead <= 0) break; uTotalRead += iRead; if (uTotalRead >= sizeof(sz)) break; } ptr->close(); serv.close(); str = sz; if (str != "1234567890987654321") goto _ERROR; return 0; _ERROR: m_fSuccess = false; return 0; } public: bool isSuccess() const { return m_fSuccess; } } srv(22000); elw::Thread thrd0(&srv); thrd0.start(); elw::Thread::sleep(250); elw::Thread thrd1(&clnt); thrd1.start(); thrd1.join(); thrd0.join(); if (!clnt.isSuccess()) return false; if (!srv.isSuccess()) return false; return true; }
int main() { printf("\n============== sizeof(boost::thread) ============== %d\n", sizeof(boost::thread)); printf("\n============== sizeof(boost::detail::thread_data_base) ============== %d\n", sizeof(boost::detail::thread_data_base)); printf("\n============== sizeof(boost::detail::thread_data<>) ============== %d\n", sizeof(boost::detail::thread_data<void(*)()>)); printf("\n============== Before thread creation ==============\n"); display_mallinfo(); { boost::thread thrd1(&count); printf("\n============== After thread creation ==============\n"); display_mallinfo(); boost::thread thrd2(&count); printf("\n============== After thread creation ==============\n"); display_mallinfo(); boost::thread thrd3(&count); printf("\n============== After thread creation ==============\n"); display_mallinfo(); thrd1.join(); printf("\n============== After thread join ==============\n"); display_mallinfo(); thrd2.join(); printf("\n============== After thread join ==============\n"); display_mallinfo(); thrd3.join(); printf("\n============== After thread join ==============\n"); display_mallinfo(); } printf("\n============== After thread destruction ==============\n"); display_mallinfo(); { pthread_attr_t attr; pthread_attr_init(&attr); pthread_t thrd1; pthread_create(&thrd1, &attr, &count2, 0); printf("\n============== After thread creation ==============\n"); display_mallinfo(); pthread_t thrd2; pthread_create(&thrd2, &attr, &count2, 0); printf("\n============== After thread creation ==============\n"); display_mallinfo(); pthread_t thrd3; pthread_create(&thrd3, &attr, &count2, 0); printf("\n============== After thread creation ==============\n"); display_mallinfo(); pthread_attr_destroy(&attr); printf("\n============== After thread attr destroy ==============\n"); display_mallinfo(); void* res; pthread_join(thrd3, &res); printf("\n============== After thread join ==============\n"); display_mallinfo(); pthread_join(thrd2, &res); printf("\n============== After thread join ==============\n"); display_mallinfo(); pthread_join(thrd1, &res); printf("\n============== After thread join ==============\n"); display_mallinfo(); //pthread_destroy(&thrd1); //pthread_destroy(&thrd2); //pthread_destroy(&thrd3); } printf("\n============== After thread destruction ==============\n"); display_mallinfo(); return 1; }