Solved

Resource Sharing in Multi-Threaded App

Posted on 2000-04-17
3
158 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

20 Experts available now in Live!

Get 1:1 Help Now