Solved

multithreading in C++ using solaris

Posted on 2007-11-23
12
609 Views
Last Modified: 2010-03-23
Hi,

I would like to generate a program which will be used by multiple clients at a time. For this, I would go for a multithreading program. Please explain me this with some simple examples.

Thanks,
shankarraj
0
Comment
Question by:shankarraj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 20339641
I assume you know what multithreading means.....
Check out

www.sun.com/software/whitepapers/solaris9/multithread.pdf
developers.sun.com/solaris/library/topics/index.jsp

let me know if you need more help
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20339649
Multithreading is closely related to operating system. So, which one are you using?
0
 
LVL 86

Accepted Solution

by:
jkr earned 43 total points
ID: 20339706
Take a look at Pthreads, that library is the most commonly used multithreading environment on UN*X platforms: http://www.llnl.gov/computing/tutorials/pthreads/ ("POSIX Threads Programming")
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20339711
oops, sorry, I didn't see the "using solaris" in the title.

@jkr,
 have you any comment about this question?
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22975992.html?cid=238
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 41 total points
ID: 20339787
>> Please explain me this with some simple examples
Hmmm... multi-threaded programming is NOT something that can be demonstrated with a couple of simple examples. There are very few things (problematically speaking) that are harder than writing good, robust, safe multi-threaded code. It's easy to knock up a bit of multi-threaded code in 10 mins; however, because multi-threaded code is (by it's very nature) non-deterministic, it is a git to test! No matter how hard you or your QA team try, there's always a sneaky race condition or priority inversion waiting to catch you out.

The first rule of designing multi-threaded solutions is to try and find another way, the 2nd is to try and find another way and the 3rd is to really try to find another way. If you still think you need to do this then I suggest you check out this: -

http://docs.sun.com/app/docs/doc/816-5137

This is Sun's Multi-threaded Programming Guide. You should read it thoroughly.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20339805
I can also strongly recommend this book...

http://www.oreilly.com/catalog/multithread/

Although it is specific to Win32 programming it does a very good job of discussing all the pitfalls of multi-threaded programming and various design patterns you can adopt to try and avoid them. Most of these principles migrate well to Unix (although the primitives used to achieve them don't).
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 20339863
you might want to refer the following VxWorks APIs, you will ofcourse need to look only at the specific multithreading APIs

http://www.slac.stanford.edu/exp/glast/flight/sw/vxdocs/vxworks/OS_Libraries.htm
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20339882
The kind of problems you can run into with MT programming: -

http://en.wikipedia.org/wiki/Priority_inversion
http://en.wikipedia.org/wiki/Deadlock
http://en.wikipedia.org/wiki/Race_condition

I don't mean to be all doom and gloom, I just want to make sure you know what pain you are potentially letting yourself in for :)

On the up-side, it when it's going well it can be a lot of fun !!!

-Rx.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20339983
It's also worth looking at Boost threads. It provides a nice abstract C++ object model for multi-threaded programming that is cross-platform...

http://www.boost.org/doc/html/thread.html
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20342121
>>I would like to generate a program which will be used by multiple clients at a time.
I presume that now that you have got an idea of what multithreading means and how ito use it in Solaris, What excatly are you looking for? Are you making a chat server and client? or are you making multithreaded server which cater to different clients at a time?
Best Regards,
DeepuAbrahamK
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 41 total points
ID: 20352306
>>I would like to generate a program which will be used by multiple clients at a time.
That sounds like a client-server, but client-server actually is two programs, a server program which normally is multi-threaded (one main thread managing all other threads and the termination, one 'worker' thread for each client 'connecting to' (using?) the server. The client normally is single-threaded beside it has a GUI. Then the client has an additional worker thread to communicate with the server. A typical client server scenario is:

server:

main thread
  - inits all resources
  - creates one thread for 'accepting' new clients
  - main thread runs infinite loop interuptable by a timeout and/or user request
  - checks client queue for requests
  - processes client requests
  - creates request for client threads

worker thread (accept thread)
  - worker thread waits for incoming clients (e. g. by socket accept on a bound socket in listen mode)
  -  for each client connecting
      - create an additional thread (client thread)
      - pass the connection info to the client thread

client thread
   - waits for requests (handshake, login, authentification) from the client
   - receives requests (messages) from client
   - passes requests from a client to main thread (to a queue)
   - checks input queue for requests from main thread
   - sends messages to clients


client:

main thread:

       - runs GUI
       - on request by user creates a worker thread
       - checks for input messages from server
       - displays server messages
       - passes user requests to worker thread

worker thread:

        - connects to server
        - runs an infinite loop
        - on timeout checks for user message to send to server
        - on timeout checks for messages received from server
        - send messages to GUI thread

You best would use the pthread library as jkr has suggested. For communication sockets are the best choice.

regards, Alex  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question