I have a program that uses some form of trees, hashtables and lists and finds similarities between two files. Basically, it's something like an inverted index. The program runs fine but I want to use multithreading for parallelization.
The problem that I'm facing is that the part that i want to parallelize creates a list and does some searches at the tree that I have. This leads to incorrect results. I know that I can use mutex and lock the specific part, but this results to zero improvements in time.
So, my question is. Can I allocate memory for a specific thread? And if I can't, how can i approach this issue?
Follows the part that I want to parallelize.
List list;
HashTable tokenHash;
initializeList(list);
createHashTable(tokenHash);
char * token = strtok(cur_doc_str, " ");
while( token != NULL ) {
list = searchTokenAtStructs(token, tree, hash);
insertSearchedTokensToHashTable(tokenHash, token);
token = strtok(NULL, " ");
}
CodePudding user response:
Sure you can allocate memory for a specific thread. You can call malloc from any thread.
If you don't need to share that data across threads that's everything you need to do.
CodePudding user response:
One approach, if you're allocating a lot of the same kind of object, would be to pool the objects. Say you have a treeNode_t struct. Let each thread have its own linked list of "free" treeNode_t objects. Instead of ever calling free(treeNode), your code could put the node onto the calling thread's free list. And, before calling malloc(), you'd first check the free list, and take a node from there if one is available. Only call malloc() to get a node when the free list is empty.
But first...
Are you sure that the "incorrect" results that you're getting are because malloc() and free() aren't working correctly in your multi-threaded version? If that really is the problem, then another possibility is that you're using the wrong library. I'd be very surprised if your build tools let you write multi-threaded code without giving you a thread-safe malloc() and free(), but maybe you have to specify some compile-time or link-time option to get the thread-safe version of the library.
