I have a section of code which is really time-consuming (~2s), and can be executed without the last one have finished.
for (x = last; x <= end && !found; x++) {
found = combine(combinaciones, x, end, ¤t);
}
Note that combine()
it's the function I was talking about. But before the loop starts again (this for it's inside another loop) all the functions must have finished.
Also, combine()
receives some arguments, combinaciones
it's a pointer, and current
an integer (by reference). The integer gets modified over time (and must get modified on the other active functions) and combinaciones
it's a linked list (size of which is current-1
). The 2nd and 3rd argument are just integers. The return it's a boolean that terminates the loop, but I don't matter if they make some extra operations.
I stress the importance of current
. If current
is equal to 5, combinaciones
's 5th element will be the created, and then current
will be 6. I mean that if two functions are allocating memory they souldn't allocate to the same position.
If that can't be done I can make the operations in parallel, and then allocate the memory on the main()
.
So, I started searching about multithreading and I found <pthread.h>
, but it only worked on Linux (not on CLion). I found something caled fork, I don't know either if can be used. Would you tell me a Windows multithreading API, with an example, and the CLion implementation?
The code is huge and I don't think that would help a lot, with I have said should be enought. However, if something is unclear let me know. The code itself it's published on GitHub, but the (few) comments it has are in Spanish.
Thanks for your help!
Using Wsl (as @Singh said) I could use pthread.h
. Then I just adapted my code. A very annoying bug that I had was that "IDs" (current
variable) were assigned non-continuously (the first thread had "3", and the second one had "2"). I was able to fix it by using a global variable (pthread_mutex_t
) and pthread_mutex_lock()
, pthread_mutex_unlock()
. For more information you can still check my code on GitHub.