interthread messaging in VS/c/c++
-
What's the lightest and easiest way to pass a message from on thread to another? The sender thread must be able to do this asynchronously so there's no to minimal time delay. The receiving thread will retrieve the thread and write it to a file. The goal here is to concentrate the hiccups associated with disk i/o to the one logging thread, so that the other working threads are not hiccup prone. It's okay that the logging thread is hiccup prone since that is all it's responsible for. Thanks for any help with this.
-
What's the lightest and easiest way to pass a message from on thread to another? The sender thread must be able to do this asynchronously so there's no to minimal time delay. The receiving thread will retrieve the thread and write it to a file. The goal here is to concentrate the hiccups associated with disk i/o to the one logging thread, so that the other working threads are not hiccup prone. It's okay that the logging thread is hiccup prone since that is all it's responsible for. Thanks for any help with this.
Say you have a structure like so, accessible by both threads as a global variable:
typedef struct {
void *data;
void *next_block;
}block;The idea is to have a list of data blocks, each with a pointer to the data to be written to file, and a pointer to the next data block to be written. The sender thread will do its work, and every time data has been generated, it will allocate a new block and add its address to the
next_block
member of the previously last block. Thenext_block
member of the newly added block is set to null. The receiver/writer thread will start at the beginning of the array and continuously check ifnext_block
has been changed from a null-pointer to something else. When it's no longer null, the sender thread must have added data. This new data is written to file and may be free'd in memory. The block struct which data was written before this block's data may also be free'd now. After this, the check loop onnext_block
will continue, based on the last written block. -
Say you have a structure like so, accessible by both threads as a global variable:
typedef struct {
void *data;
void *next_block;
}block;The idea is to have a list of data blocks, each with a pointer to the data to be written to file, and a pointer to the next data block to be written. The sender thread will do its work, and every time data has been generated, it will allocate a new block and add its address to the
next_block
member of the previously last block. Thenext_block
member of the newly added block is set to null. The receiver/writer thread will start at the beginning of the array and continuously check ifnext_block
has been changed from a null-pointer to something else. When it's no longer null, the sender thread must have added data. This new data is written to file and may be free'd in memory. The block struct which data was written before this block's data may also be free'd now. After this, the check loop onnext_block
will continue, based on the last written block.Thanks, I like this solution. Additionally I'd need to manage shared access with mutexes.
-
Thanks, I like this solution. Additionally I'd need to manage shared access with mutexes.
That shouldn't even be necesary in this case: If you make sure a block is initialised (data pointer valid, next_block pointer initialised) before it's appended to the list by the sender thread, it can't cause multithreading problems since the actual appending action is just one assignment.