Menu
Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Is it possible to let the c++ compiler run tasks parallely?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="kwikius" data-source="post: 2559863"><p>FWIW I believe that Boost.Threads is well regarded, widely available</p><p>and portable as well as being discussed in the context of concurrent</p><p>programming for C++ by the standardisation committee, so IMO it is</p><p>quite acceptable to discuss in the context of the C++ language</p><p>newsgroup:</p><p><a href="http://www.boost.org/doc/html/threads.html">http://www.boost.org/doc/html/threads.html</a></p><p></p><p>Below is a program that demonstrates the sort of functionality</p><p>described by the OP. To run it you will need to download and build the</p><p>Boost.Threads threads library in the boost distro. <a href="http://www.boost.org">http://www.boost.org</a></p><p></p><p>regards</p><p>Andy Little</p><p></p><p>--------------------</p><p></p><p>#include <boost/thread/thread.hpp></p><p>#include <boost/date_time/posix_time/posix_time.hpp></p><p>#include <iostream></p><p>#include <string></p><p>/*</p><p> thread demo. Give user 7 seconds to enter a string</p><p>*/</p><p>struct call_back_timer{</p><p> typedef boost:<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" />osix_time::time_duration time_duration;</p><p> typedef boost:<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" />osix_time:<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" />time ptime;</p><p> typedef boost:<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" />osix_time::second_clock second_clock;</p><p> static void reset_flag()</p><p> {</p><p> flag = false;</p><p> }</p><p> static void set_flag()</p><p> {</p><p> flag = true;</p><p> }</p><p> call_back_timer(</p><p> time_duration const & p, // interval</p><p> int c, // number of cycles to repeat</p><p> void(*f1)(int current,int tot ), // per cycle function with</p><p>count arg</p><p> void(*f2)() // end function</p><p> )</p><p> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" />eriod(p),count(c),pf1(f1),pf2(f2){}</p><p>private:</p><p> int count;</p><p> static bool flag;</p><p> const time_duration period;</p><p> void(*pf1)(int n,int count);</p><p> void(*pf2)();</p><p>public:</p><p> void operator()()</p><p> {</p><p> ptime t = second_clock::local_time();</p><p> for (int n = 0; n <= count;++n){</p><p> t += period;</p><p> while( second_clock::local_time() < t)</p><p> {</p><p> if (!flag){</p><p> return;</p><p> }</p><p> }</p><p> pf1(n,count);</p><p> }</p><p> pf2();</p><p> }</p><p>};</p><p></p><p>bool call_back_timer::flag = true;</p><p></p><p>void print_time(int n,int count)</p><p>{</p><p> if ( (count -n) > 0){</p><p> std::cout << "you have "<< (count - n) << " seconds left\n";</p><p> }</p><p> else {</p><p> std::cout << "\nSorry... too late. Press return to quit\n";</p><p> }</p><p>}</p><p></p><p>struct user_input{</p><p>private:</p><p> static bool flag;</p><p> void(*pf)();</p><p>public:</p><p> static void set_flag()</p><p> {</p><p> flag = true;</p><p> }</p><p> static void reset_flag()</p><p> {</p><p> flag = false;</p><p> }</p><p> user_input(void(*f)())<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" />f(f){}</p><p> void operator()()</p><p> {</p><p> std::cout << " Enter a string and press return: ";</p><p> std::string str;</p><p> getline(std::cin, str);</p><p> if (flag){</p><p> std::cout << "\nyou entered '" << str << "'\n";</p><p> }</p><p> pf();</p><p> }</p><p>};</p><p></p><p>bool user_input::flag = true;</p><p></p><p>int main()</p><p>{</p><p> call_back_timer timer(</p><p> boost:<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" />osix_time::seconds(1),</p><p> 5,</p><p> print_time,</p><p> user_input::reset_flag</p><p> );</p><p> timer.set_flag();</p><p> user_input user(call_back_timer::reset_flag);</p><p> user.set_flag();</p><p> boost::thread_group thrds;</p><p> thrds.create_thread(user);</p><p> thrds.create_thread(timer);</p><p> thrds.join_all(); </p><p>}</p></blockquote><p></p>
[QUOTE="kwikius, post: 2559863"] FWIW I believe that Boost.Threads is well regarded, widely available and portable as well as being discussed in the context of concurrent programming for C++ by the standardisation committee, so IMO it is quite acceptable to discuss in the context of the C++ language newsgroup: [URL]http://www.boost.org/doc/html/threads.html[/URL] Below is a program that demonstrates the sort of functionality described by the OP. To run it you will need to download and build the Boost.Threads threads library in the boost distro. [URL]http://www.boost.org[/URL] regards Andy Little -------------------- #include <boost/thread/thread.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <iostream> #include <string> /* thread demo. Give user 7 seconds to enter a string */ struct call_back_timer{ typedef boost::posix_time::time_duration time_duration; typedef boost::posix_time::ptime ptime; typedef boost::posix_time::second_clock second_clock; static void reset_flag() { flag = false; } static void set_flag() { flag = true; } call_back_timer( time_duration const & p, // interval int c, // number of cycles to repeat void(*f1)(int current,int tot ), // per cycle function with count arg void(*f2)() // end function ) :period(p),count(c),pf1(f1),pf2(f2){} private: int count; static bool flag; const time_duration period; void(*pf1)(int n,int count); void(*pf2)(); public: void operator()() { ptime t = second_clock::local_time(); for (int n = 0; n <= count;++n){ t += period; while( second_clock::local_time() < t) { if (!flag){ return; } } pf1(n,count); } pf2(); } }; bool call_back_timer::flag = true; void print_time(int n,int count) { if ( (count -n) > 0){ std::cout << "you have "<< (count - n) << " seconds left\n"; } else { std::cout << "\nSorry... too late. Press return to quit\n"; } } struct user_input{ private: static bool flag; void(*pf)(); public: static void set_flag() { flag = true; } static void reset_flag() { flag = false; } user_input(void(*f)()):pf(f){} void operator()() { std::cout << " Enter a string and press return: "; std::string str; getline(std::cin, str); if (flag){ std::cout << "\nyou entered '" << str << "'\n"; } pf(); } }; bool user_input::flag = true; int main() { call_back_timer timer( boost::posix_time::seconds(1), 5, print_time, user_input::reset_flag ); timer.set_flag(); user_input user(call_back_timer::reset_flag); user.set_flag(); boost::thread_group thrds; thrds.create_thread(user); thrds.create_thread(timer); thrds.join_all(); } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Is it possible to let the c++ compiler run tasks parallely?
Top