[SOLVED] C++ pthread (an attempt to do a multithreading function)

Edgamer63

Member
Hello, i don't know what is happening right now... because i'm doing a dll function that should be doing a task in another thread.
My actual attempt is to make it work after compile, because yes, it compiles... but it throws me an error "LoadLibraryA failed with error code 126" and this only happens when i put a line like : "pthread_create" or "pthread_detach", and it's weird, because in a console application it does work, but when trying to implement this on a dll... nothing happens, and also makes the dll unusable.

Well, i will bring a clearly reproducible example, like:

Console application (Works):
C++:
#include <pthread.h>
#include <cstddef>
#include <iostream>

using namespace std;

void* process_mt(void* input){
    int in = 4;
    cout << "OTHER! \n" << endl;
    pthread_exit((void*) in);
    return nullptr;
}


int main()
{
    pthread_t th;
    pthread_create(&th, NULL, process_mt, NULL);

    cout << "Hello world! \n" << endl;
    return 0;
}
well, now the thing i tryed with the dll (Giving me troubles and i don't know why):
C++:
#include <pthread.h>
#include <cstddef>
#include <iostream>

#define GMEXPORT extern "C" __declspec(dllexport)
using namespace std;

void* process_mt(void* input){
    int in = 4;
    cout << "OTHER! \n" << endl;
    pthread_exit((void*) in);
    return nullptr;
}
//*/
GMEXPORT double multithread_test(void* Buffer){
    pthread_t th;
    pthread_create(&th, NULL, process_mt, NULL);//This don't work, and i don't know why

    return 102;
}
So yes, it returns 0 instead of 102, because of the loadlibrary error... So, i don't know what to proceed in this case.
 
You can't return NULL in a void function. Not sure if everything else is working, but this jumps at me right away!
C++:
void* process_mt(void* input){
    int in = 4;
    cout << "OTHER! \n" << endl;
    pthread_exit((void*) in);
    return nullptr;                   
}
 

GMWolf

aka fel666
You can't return NULL in a void function. Not sure if everything else is working, but this jumps at me right away!
C++:
void* process_mt(void* input){
    int in = 4;
    cout << "OTHER! \n" << endl;
    pthread_exit((void*) in);
    return nullptr;                  
}
It's a void*. So its returning a void pointer.


It's possible the pthread library isn't loaded by Game maker.
You need to explicitly state that your library depends on pthreads when linking it.
 
It's a void*. So its returning a void pointer.


It's possible the pthread library isn't loaded by Game maker.
You need to explicitly state that your library depends on pthreads when linking it.
Really?
I've always just seens this syntax
Code:
void *ptr{ nullptr }
...honestly thought "return" would break it, but yeah, a glance in the book says it doesn't matter!
Thanks for correcting me!
 

Edgamer63

Member
Could you post your solution for future generations?
I was doing it but i go for lunch and i returned just for that. I think it will be worth a tutorial on how to use pthreads in code::blocks for gamemaker in the future ;) . Well, the solution (in code::blocks) was to :

Add the library to dependencies.

1.- Open Build options...
1616950493600.png

2.- Click "add"

1616950622577.png

3.- Include the libraries installed of pthread:

1616950701000.png
then click open (in my case it says "abrir", because i speak natively spanish (i'm chilean :D )).

4.- I recommend you to select "no" in this dialog.
1616951014933.png
 

Attachments

Top