Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Resource Sharing in Multi-Threaded App

Posted on 2000-04-17
3
Medium Priority
?
166 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 400 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 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