I am working on a performance critical system that has a lot of logging. I am planning to do my major computation in a thread that sticks to one core, and logs in another thread that sticks to another core. How do I achieve that in pthread_setaffinity_np() ?
I imagine that my code would look like
void log_something(const string& st) {
pthread_setaffinity_np(pthread_self(),sizeof(cpuset),&cpuset);
//LOG string st;
}
int main() {
while (true) {
// do some computation
log_something(something)
}
}
My question is, when I call pthread_setaffinity_np() in log_something(), would it do the logging in the cpu I specified, and while doing the logging, return to the while loop of main() and continue the computation?
Or it will switch the entire program to that cpu set, and will only return to main method after logging?
Thanks!
[EDITED] I use logging as an example but my practical problem is more complicated than that. For example, I might have to update a dynamic parameter every minute, and while I am updating the parameter, I still want to continue my computation in the main() method based on the old parameter (i.e. I just can't stop my main computation for parameter updating). Therefore the process of updating the parameters might have to be migrated to another thread that sticks to another core. So I am looking for a generic solution of separating the computation, not just an efficient logger.
Sorry for the confusion.