Click here to Skip to main content
15,924,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,
am new to MFC.i have done worker thread.
one thread prints the natural numbers by creating one .txt file and my intention is to open the same file and print even numbers .
am able to print in different files by creating new .txt file in new thread.
but i need the same file(which is created by first thread) to be opened and prints even numbers.
please help me out.
Posted
Updated 11-Jun-13 23:37pm
v3
Comments
Richard MacCutchan 12-Jun-13 4:19am    
Your question is not very clear; why are you using different threads?
Member 9902987 12-Jun-13 4:48am    
Actually am new to MFC.so am learning threads .for learning am trying with sample program so i want to print the way i asked sir.
Richard MacCutchan 12-Jun-13 5:09am    
You would be better off getting your basic functionality to work, before trying to use multiple threads. However, if you want a single thread to handle the writing of all your data then you need to learn about Synchronization Objects.
Member 9902987 12-Jun-13 5:16am    
below program is for threads...so please help me to write into same file.
Jochen Arndt 12-Jun-13 5:25am    
Please don't enhance your question by adding additional information as solution. Use the green 'Improve question' link instead to edit the question. I have copied the code from your solution and formatted it. So you should delete your solution now.

1 solution

In each thread, whenever you want access to the file:

  1. Before accessing the file, using the Windows API, create a named mutex (giving it the whole path to your file, case sensitive) and acquire it.
  2. Using C++ standard IO, open the file.
  3. Using C++ standard IO, write to the file.
  4. Using C++ standard IO, close the file.
  5. Release (don't destroy) the mutex

This is the most basic threading pattern - locking. It has serious disadvantages (performance wise), since threads are locked waiting for the mutex, so in fact your program is no longer multithreaded.

Another pattern:

  • Create a thread whose only purpose will be managing access to the file.
  • Create a queue of commands (you may use the STL std::queue); this queue should be self-locking.
  • Whenever a client wants to write someting to the file, add a 'please write' command to the queue.
  • Have your file access pop commands from the queue, and execute them one by one.
  • Whenever a client wants to read something from the file, add a 'read' command to the queue.
  • The 'read' command should include a callback (pointer to interface, usually) to execute when the 'read' command is popped from the queue and executed.


This is more elaborate, and less blocking.
In general, do not try to write multi threaded code until you have read at least 400 pages (that's one book, or a lot of articles) about multi threading design. You may google: 'lock free multi threading patterns', 'windows threads', 'parallel programming'.

Hope this helps,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900