Solved

Resource Sharing in Multi-Threaded App

Posted on 2000-04-17
3
165 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
[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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

631 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