Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Resource Sharing in Multi-Threaded App

Posted on 2000-04-17
3
Medium Priority
?
171 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

926 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