I have a rather large, dynamic sparse matrix object class to write, and I want to make the following happen: one thread to handle placing elements into the matrix, and one to handle reading from the matrix.
The only time when these two would conflict would be when they would both want to access the same row/column at the same time. As such, I've decided that a simple mutex lock for each row/column is sufficient.
Now this is the first time I've actually done threading in C/C++, and I'd like to do it by the books, so to speak. I have two concerns.
- How do I spawn these threads? This is a language question more than anything.
- How do I implement the locking itself as efficiently as possible? I figure if there is a conflict, then the requesting thread would place itself in line and wait until the resource is freed. However, how do I implement that waking? I can have a loop poll the memory location, but that's not elegant. Ideally, I figure an interrupt based approach would be best.