Solved

Resource Sharing in Multi-Threaded App

Posted on 2000-04-17
3
159 Views
Last Modified: 2010-04-02
Object x contains an object that provides access to a serial com port.   When a particular event occurs, object x creates a thread that access this serial com port.  When the thread is done accessing the com port, it dies.  This process is repeated over  time.

While, the first thread successfully accesses the com port, when the next thread accesses the com port, there is no system failure, but all the read/write commands to the com port do not work.

Does a handle or something need to be passed between threads to allow them to share the serial com port?
0
Comment
Question by:joex
  • 3
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2722499
Are you opening the COM port with CreateFile()?  If so the 2nd time you try to open it, CreateFile should fail and you shoudl get an invalid handle, this handle will be ignored when you try to read/write to it.  If you are not usig CreateFile(), but are using some other procedure, it will (ultimately) use CreateFile which will fail too, so this procedure you are using should be failing, if there is a way to detect that.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2722507
If you wisht to allow multiple threads to use the handle, you will need to store the handle in a global location so that they can all obtain it.  You can use reference counting to help manage this.  i.e. you store global count of the number of threads using the handle.  (When the count is 0, no threads are using the handle and the the handle is closed.)  When a thread needs to use the handle, if the count is 0, meaning the handle is closed, the thread opens the handle and stores it in the global location.  Then in either case (if the count was 0 or wasn't 0) the thread increments the count.  When a thread is done with the handle it decrements the count.  If the count drops to 0, meaning no other threads are using the handle anymore, the thread also closes the handle.  Now since this uses shared global resources, this resources must be protected by a synchronization mechanism, like a critical secton.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2722522
However, it is very unusual to have a case where multiuple threads can read/write from the serial port at the same time.  Are you sure your design really can support that?  What if thread A needs to write 10 bytes, but in the middle is suspended and thread B writes information.  Will that be okay?  Thread A's information will have other information in the middle.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

862 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now