0

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, &current);
}

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!

  • 1
    You should just use Wsl and use wsl toolchain , for more look here :https://www.jetbrains.com/help/clion/how-to-use-wsl-development-environment-in-clion.html – Singh Mar 25 '20 at 12:58
  • @Singh OK, now `pthread.h` works, but I still don't have the code. I'll try to do it on my own, if I succeed I will update the post. – Roger Miranda Perez Mar 25 '20 at 14:13
  • this maybe could help you : https://stackoverflow.com/a/11624560/9386720 – Singh Mar 25 '20 at 14:38
  • regarding: `typedef unsigned char *Encrypted;` It is a very poor programming practice to hide pointers in a `typedef` statement – user3629249 Mar 26 '20 at 17:43
  • regarding: `typedef struct {` Most debuggers use a struct `tag` name to access the individual fields within the struct. Therefore, strongly suggest giving the struct a `tag` name – user3629249 Mar 26 '20 at 17:48
  • in function: `myFun()`, the preferred method of returning from a thread is not : `return NULL;` but rather: `pthread_exit( NULL );` – user3629249 Mar 26 '20 at 17:52
  • in file: `paraleling.h` the prototype for a function that does not receive an parameters needs to have `void` between the parens. otherwise, the compiler produces a function that can receive any number of parameters (including none) Suggest replacing: `void paralel();` with `void paralel( void );` – user3629249 Mar 26 '20 at 17:55
  • regarding: `printf("[e] On joining thread\n");` Error messages should be output to `stderr`, not `stdout`. Suggest: `fprintf( stderr, "[e] On joining thread\n %s\n", strerror( ) );` Of course this means that number needs to be actually saved into a variable. from the MAN page: *On success, pthread_join() returns 0; on error, it returns an error number.* – user3629249 Mar 26 '20 at 17:58
  • regarding: `printf("[v] Combinaciones: %lu\n", *comb);` This means the reading/writing of the variable needs to be protected, so no 'race' conditions result. Suggest using a `pthread_mutex` for this purpose. Also suggest `sleep(30)` be replaced with the call to lock the mutex and after the following printf() statement a call to unlock the mutex – user3629249 Mar 26 '20 at 18:05
  • in function: `processParalel()` once a call to `pthread_create()` fails, it is very unlikely that another call to that function will succeed. – user3629249 Mar 26 '20 at 18:20
  • in `paraleling.c`, the variable `start` is declared by: `clock_t start;` however, it is being set via: `start = (float)clock();` The cast to `float` needs to be removed. – user3629249 Mar 26 '20 at 18:37
  • when calculating an elapsed time in seconds, use the function: `difftime()` so the calculation would be: `printf("[v] Tiempo de ejecucion medio: %.2fs\n", difftime( clock() , start) / CLOCKS_PER_SECOND / THREADS);` which allows for the value returned by `clock()` needs to be treated as an unsigned value – user3629249 Mar 26 '20 at 18:44
  • regarding: `int main(int argc, char **argw) {` Because those parameters are not used, the compiler will output two warnings messages about unused parameters. To fix these warnings, use the other valid signature for `main()` `int main( void )` – user3629249 Mar 26 '20 at 18:57
  • when compiling, always enable the warnings, then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same results. – user3629249 Mar 26 '20 at 19:05
  • @user3629249 sorry for the reply delay. Thanks for all. I'll consider them for future projects. – Roger Miranda Perez Apr 20 '20 at 15:34

1 Answers1

0

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.

  • I'm glad that you solve your problem by yourself, early i forgot to tell you about pthread mutex but im happy you figured it out ;). – Singh Mar 25 '20 at 20:40