Hello all .
Im starting to learn boost , and now im on threads.
and for the time being , im trying to use two threads to sum lets say 1000000 numbers as an example , i want these threads iterate through a loop , one after another , i mean if thread one counted to 3 , thread 2 continues with 4 and so on ,how can i share that variable with these threads ? how can i do that ?
i already tried to use mutex .
but it seems im doing it wrong! , i also tried using the condition variable (condition_variable_any) so that threads can notify eachother and thus speed up the process but im stuck how to use wait(mutex) in first place cause there is only one function!Code://in the name of GOD #define BOOST_THREAD_USE_LIB #include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/thread.hpp> boost::uint64_t i = 0; boost::uint64_t sum=0; boost::mutex mutex; void IteratorFunc() { mutex.lock(); for (i ; i<1000000000 ; i++) { sum+=i; } mutex.unlock(); } using namespace std; int main() { boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time(); boost::thread thread(IteratorFunc); boost::thread thread2(IteratorFunc); boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time(); cout << "sum =\t" << sum<< "\t"<<end-start<<endl; thread.join(); thread2.join(); return 0; }
i dont want to separete the loop into two functions! i just want that one function to be used by two threads in the specified manner
can someone help get this going ?
------
how can i get all threads ID issued by me in my app?
how can i iterate through running threads in my app ?
is there any kind of means to get all the running threads using boost library? if it does whats the calss? if it doesnt how can i do that?
can i resume a thread after pausing it ? ( how can i pause a thread rather than using sleep? )
thank you in advance