Urien
Urien

Reputation: 33

pthread_create buffer size. Not enough?

I'm trying to create a thread with pthread_create function. The call is something like that: res pthread_create(&threadID, &atributte, function, argument)

res value is always 0 which means that pthread_create launch the thread right. The problem, I think, is in the last parameter (argument). It is a too loooooong string and I think this is crashing my program because if I try with a shorter string It's all correct and ends properly.

I was wondering if pthread_create creates a thread with a certain amount of memory and If any of you know that amount.

PD: My log doesn't show anything, I put a cout at the first line of function but is never readed because my program crashes before it happens.

Thank you all!

Edit: Rest of the code, how i create the thread.

    void znkModulePlugin::createMainProcess(void* mod){
znkModulePlugin* module = static_cast<znkModulePlugin*>(mod);
try{
    int res = 0;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    res = pthread_create(&threadID, &attr, znkModulePlugin::launchThread, module); 
}catch(exception e){
    stringstream log_msg;
    log_msg << " CRIT znkModulePlugin::createMainProcess() -> Critical Error: " << e.what() << ". Reseting Node.";
    logData(log_msg.str());
}

}

Upvotes: 0

Views: 247

Answers (1)

David Schwartz
David Schwartz

Reputation: 182753

Follow this pattern to avoid problems:

1) Allocate a new object/struct holding the information the new thread needs.

2) Call pthread_create passing it a pointer to the new object.

3) When the thread is done accessing the information, delete the object/struct.

If you pass a thread a pointer to something, you must make sure that pointer remains valid (and its contents unmodified unless synchronized) until the new thread is done with it.

Upvotes: 1

Related Questions