943,155 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4333
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 27th, 2009
1

How to do multithread programming in C++..

Expand Post »
i DO have searched the google to do multithreaded programming using c++ and found there were few like the ones supported in BOOST libraries and zthreads ..
I would be glad if u guys could help me in suggesting much better ways in doing so .
Similar Threads
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Dec 27th, 2009
0
Re: How to do multithread programming in C++..
What OS?
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Dec 27th, 2009
0
Re: How to do multithread programming in C++..
i am ok with windows or linux .
Since i am a naive in this field i would prefer something that is basic and easy to use.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Dec 28th, 2009
0
Re: How to do multithread programming in C++..
Just use CreateThread() -- its simple to use because most of the parameters are 0 or NULL. I have not used the boost library, so I don't know how they work.

Creating threads is easy, making them work together may be more problematic. If two or more threads have to access a common object then you will have to implement thread synchronization via mutex or critical sections.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2281
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,938 posts
since Aug 2005
Dec 28th, 2009
0
Re: How to do multithread programming in C++..
i theoretically understand mutex , semaphores and the related concepts as i had a complete subject on OS this semester .
I have devised my own methods of theoretically solving dinner philosophers problem and readers/writers problems which are very well know in deadlocks
But now i want to do this thing practically , just don't have any idea on how to proceed.
I would be grateful if u could enlighten me on how to start with this.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Dec 28th, 2009
0
Re: How to do multithread programming in C++..
Aren't there any examples in your OS-course book? I had an OS course(outside of my normal curriculum as I study mechanical engineering) too this semester, which included a 'lab' where we were given the task to write a multi threaded assignment.
I would suggest using the 'pthread' library, especially in Linux it is commonly used as the p stands for POSIX.
You can add threads easily to your application but as Dragon said.. the fun part starts when they share resources.

I don't think it is necessary to explain pthread, there are tons of good tutorials on the web.
Reputation Points: 193
Solved Threads: 75
Posting Pro in Training
thelamb is offline Offline
426 posts
since Aug 2008
Dec 28th, 2009
0
Re: How to do multithread programming in C++..
Click to Expand / Collapse  Quote originally posted by rahul8590 ...
i DO have searched the google to do multithreaded programming using c++ and found there were few like the ones supported in BOOST libraries and zthreads ..
I would be glad if u guys could help me in suggesting much better ways in doing so .
Hi,

Firstly, wait! Do not EVER use CreateThread(). I say it because it's an API call and will be available for execution, even for a program which uses the single threaded version of the CRT! The results may not be pleasing. By calling the compiler provided function to create a thread, you will never be able to commit such a blunder, because the compiler will catch you (the thread creation call won't be available for use while linking to the single threaded version of the CRT). Also, initializing the CRT won't happen with CreateThread(). The MSDN documentation for CreateThread() neglects to mention this extremely important trait.

If you're using the Visual C++ compiler, you should use _beginthread or _beginthreadex (recommended) or if you're using MFC, you MUST use AfxBeginThread. If you're using a different compiler, check with the compiler documentation to find out what function does it provide to create threads.

Explaining these things in detail is possibly out of the context of this thread.

There are several good articles allover the web. Here's one for instance: Multithreading Tutorial

Here's a whole lot of stuff: Threads, Processes and IPC section

Threading is an extremely interesting aspect of programming. Have fun.
Reputation Points: 137
Solved Threads: 3
Junior Poster in Training
Rajesh R Subram is offline Offline
62 posts
since Dec 2009
Dec 28th, 2009
0
Re: How to do multithread programming in C++..
thanks guys , with ur help i could write my first thread program , but here is the problem .


i got the first program

here is the code
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<windows.h>
  3. #include<process.h>
  4.  
  5. using namespace std;
  6.  
  7. void silly (void *);
  8.  
  9. int main ()
  10. {
  11. cout<<" now in main function ";
  12.  
  13. _beginthread( silly , 0 , (void *)12) ;
  14.  
  15. silly ( (void *) -5 ) ;
  16. Sleep (100) ;
  17. return 0;
  18. }
  19.  
  20. void silly ( void * arg )
  21. {
  22. cout<<" this is the silly function "<< (INT_PTR)arg ;
  23. }


i use MInGW compiler and currently working in windows

but the problem is the output was something like this

Quote ...
now in main function t thhisi si si sthe sitllyh ef uncstioninlly function1-2
5
honestly i dont even know whether this output is correct or not , and if this is the right one well then why is it executing so weirdly ?
Last edited by rahul8590; Dec 28th, 2009 at 1:04 pm. Reason: corrected the code myself .. sorry was a ridiculous mistake
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Dec 28th, 2009
0
Re: How to do multithread programming in C++..
Click to Expand / Collapse  Quote originally posted by rahul8590 ...
thanks guys , with ur help i could write my first thread program , but here is the problem .


i got the first program

here is the code
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<windows.h>
  3. #include<process.h>
  4.  
  5. using namespace std;
  6.  
  7. void silly (void *);
  8.  
  9. int main ()
  10. {
  11. cout<<" now in main function ";
  12.  
  13. _beginthread( silly , 0 , (void *)12) ;
  14.  
  15. silly ( (void *) -5 ) ;
  16. Sleep (100) ;
  17. return 0;
  18. }
  19.  
  20. void silly ( void * arg )
  21. {
  22. cout<<" this is the silly function "<< (INT_PTR)arg ;
  23. }


i use MInGW compiler and currently working in windows

but the problem is the output was something like this



honestly i dont even know whether this output is correct or not , and if this is the right one well then why is it executing so weirdly ?
Umm... The output was something like what?! The output was blank?

Which might possibly mean that the program (the main thread) terminated before the worker thread was scheduled to run (or after the worker thread was scheduled to run AND before it actually started to run).

I seriously think that I cannot be teaching you the asynchronous nature of threaded applications, context switching and other gotchas involved in multithreading here in a post like this.

I suggest that you read a good book on the said topic. It will cover things in an orderly fashion, and with suitable examples.
Last edited by Rajesh R Subram; Dec 28th, 2009 at 1:12 pm.
Reputation Points: 137
Solved Threads: 3
Junior Poster in Training
Rajesh R Subram is offline Offline
62 posts
since Dec 2009
Dec 28th, 2009
0
Re: How to do multithread programming in C++..
the output is mentioned in my last edited post.
Well sorry for that .

I have read OS by GALVIN , but that is not suitable for pragmatic programmers like me .
can u suggest some good books which practically teaches on how to implement this and also incorporate valuable topics like IPC and MPI .
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Tic Tac Toe help
Next Thread in C++ Forum Timeline: Does the following compiler accept vectors?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC