Multi-thread, read/write to single file.

B

Boki

Multi-thread read/write to a single file.

I have two processing threads, thread A and thread B; and I called my
queue as Q.

Thread A will feed data into Q by user input, the timing is random.
Thread B will read data from Q to process; this processing will
communicate with a website.

I think the best Q architecture is ring buffer as large as possible.
However, my idea is to create a textbox.

The problem is the two threads might read/write to Q at the same time.
So, what I need to do is to suspend thread whenever user is submitting
data to Q?

How about when thread B is deleting data (because the data was
processed), I might need to suspend thread A, but thread A is user
interface, there should never a latency.

So, my idea is, UI should separate from processing thread.

Please give me any of your ideas.

Thanks!

Best regards,
Boki.
 
G

Guest

It sounds like you are saying you have a class A that directly manipulates
the queue, as does class B, rather than both interacting through methods on a
class Q. I believe A and B should not care about the workings of Q. A
should simply call Add(xyz), while B should call Subtract() that returns the
next item (and pulls the data of the queue internally).
 
P

Peter Duniho

[...]
The problem is the two threads might read/write to Q at the same time.
So, what I need to do is to suspend thread whenever user is submitting
data to Q?

There are a variety of ways to do this. If one of the threads is the main
GUI thread, then you can use Control.Invoke() or Control.BeginInvoke()
from the other thread to add things to queue. These will be assured of
not executing simultaneously with other work you do on the main thread, by
simple virtue that all the work happens on the same thread.

If neither thread is the main GUI thread, then you need to use some sort
of synchronization mechanism. The simplest is to use the "lock()"
statement. You can find documentation for it on the MSDN web site. See
also things like the Monitor class, and various "synchronization objects"
also documented on MSDN (WaitHandle, Mutex, Semaphore, and maybe others
I've forgotten, depending on your needs).

Pete
 
B

Boki

It sounds like you are saying you have a class A that directly manipulates
the queue, as does class B, rather than both interacting through methods on a
class Q. I believe A and B should not care about the workings of Q. A
should simply call Add(xyz), while B should call Subtract() that returns the
next item (and pulls the data of the queue internally).

Ya, I just finished the code, and the result works well.

It sounds great, but I am confused, we don't need to consider the
conflict on reading/writing to a same file at the same time ?

Because I temporally test with a textbox ( however, it works well, but
I feel there is a automatically delay by windows OS, not my code
controlling ).

Speaking to the architecture, finally, I will implement to a real text
file on the storage, I think in the future, more threads ( maybe more
execution files ) are reading/writing to same file at the same time.

I think I might need to consider this issue (?)

Best regards,
Boki.
 
P

Peter Duniho

There are a variety of ways to do this. If one of the threads is the
main GUI thread, then you can use Control.Invoke() or
Control.BeginInvoke() from the other thread to add things to queue.

Addendum:

Of course, if the other thread is the one that removes things rather than
add things, then that would work fine too. The key is synchronizing
access by ensuring that all access happens on the same thread.
 
B

Boki

Addendum:

Of course, if the other thread is the one that removes things rather than
add things, then that would work fine too. The key is synchronizing
access by ensuring that all access happens on the same thread.

You are right, thanks a lot!

Best regards,
Boki.
 
G

Guest

Are you trying to store the state of the queue, or are you trying to use a
file to maintain the queue state?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Boki said:
Multi-thread read/write to a single file.

I have two processing threads, thread A and thread B; and I called my
queue as Q.

U mean that you are ACTUALLY using a queue right?
If that is so, use a synced queue (by using Queue.Synchronized(
yourQueue) ).
After that all the operations will be thread safe.

I think the best Q architecture is ring buffer as large as possible.

Humm, what about using your synced Queue?
However, my idea is to create a textbox.

You lost me here, what a textbox has to do with this?
The problem is the two threads might read/write to Q at the same time.
So, what I need to do is to suspend thread whenever user is submitting
data to Q?

If you use a sync'ed Queue the framework will take care of that for you.
How about when thread B is deleting data (because the data was
processed), I might need to suspend thread A, but thread A is user
interface, there should never a latency.

If you use a sync'ed Queue you can access it from both threads without any
problem.
 
B

Boki

Hi,





U mean that you are ACTUALLY using a queue right?
If that is so, use a synced queue (by using Queue.Synchronized(
yourQueue) ).
After that all the operations will be thread safe.


Humm, what about using your synced Queue?


You lost me here, what a textbox has to do with this?


If you use a sync'ed Queue the framework will take care of that for you.


If you use a sync'ed Queue you can access it from both threads without any
problem.

Hi

I don't know this method before, I am checking it, thanks a lot!

Best regards,
Boki.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top