Thread synchronization problems

A

Allen

I use a semphore to synchronize memory map file access.
The LogWriter thread writes log messages into the map file.
And the LoggerDaemon thread reads log messages from the map file.

When the LogWriter thread writes log messages with Sleep(5) in the
main loop, it is ok.
By to test extreme situation, I write log messages without sleep. And
there will be some
problems occurs when accessing map file for both writer and reader
sides. For example,
the message count will turn to be negative unexpectedly.

I add printf in the writer loop, the writer will be ok. And add printf
in the reader loop,
the reader will be ok.

Who can tell me some clue to find the solution?
Thank you.
 
J

Joe Seigh

Allen said:
I use a semphore to synchronize memory map file access.
The LogWriter thread writes log messages into the map file.
And the LoggerDaemon thread reads log messages from the map file.

When the LogWriter thread writes log messages with Sleep(5) in the
main loop, it is ok.
By to test extreme situation, I write log messages without sleep. And
there will be some
problems occurs when accessing map file for both writer and reader
sides. For example,
the message count will turn to be negative unexpectedly.

I add printf in the writer loop, the writer will be ok. And add printf
in the reader loop,
the reader will be ok.

Who can tell me some clue to find the solution?
Thank you.

Try an fflush before the sleep. You should probably learn
how stdio buffering works.
 

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

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,180
Latest member
DelmarCarv

Latest Threads

Top