Reputation: 11
Programming in C in userspace.
I have a scenario where I am parsing (string manipulations) a text file(infile) to create another text file (outfile).
The Design I have in mind is :
I keep reading character by character to form a line from inFile. Create a thread to do some string manipulations and eventually that thread would end up writing the new line to outfile.
Obviously I would have to synchronize the threads that are writing to outfile; but I also have to synchronize them in such a way that if thread1 parses line1 and thread2 parses line2; thread1 should be the one acquiring the lock first to write to outfile. This is so that the text in outfile matches the text line in infile.
Basically have a mechanism for something like a ticket and the locking should check the ticket.
How can I achieve this in C?
Upvotes: 1
Views: 56
Reputation: 198344
You can have a queue in main thread. Don't write in worker threads, have the main thread in charge of reading and writing. When you launch a thread, put the thread in the queue. When you have launched all the threads you want, you can loop on the queue and join the threads in order to acquire their results, writing the output. (If you want to limit the number of threads, limit the size of the queue, and launch a new thread in the tail of the queue when you process the head of the queue.) Something like this:
for queue_size:
read line
if no more lines:
mark end of file
break
launch processing thread
put thread in queue
while queue not empty:
pop thread
join thread
get results
write results
if not end of file:
read line
if no more lines:
mark end of file
continue
launch processing thread
put thread in queue
pthread_join
will block the main thread till the next line is processed; meanwhile, the lines further down will continue processing in the background, or wait to be joined.
Upvotes: 1
Reputation: 118691
You don't do it that way. If you do, everyone will be blocked and you'll just serialize the entire process.
Instead, you have the parsing threads post their results to a (thread safe) priority queue which is then read by a single thread to write to the file.
The key factor is that the parsed lines go to the priority queue with their line number being the component to determine priority, and the thread that reads the queue will keep track of the last line number it saw. Then it peeks in to the top of the queue, checks if it's the "next line". If it is, it removes it from the queue and writes to the file. If not, it stalls for a moment (a quick sleep perhaps, or somehow waits for the next queue insertion) until the top of the queue is in fact the next line that it's looking for.
Upvotes: 3