Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Gdzie jest błąd klasa z thread zle wyświetlającza wartości

Ostatnio zmodyfikowano 2017-09-28 17:15
Autor Wiadomość
Breakermind
Temat założony przez niniejszego użytkownika
Gdzie jest błąd klasa z thread zle wyświetlającza wartości
» 2017-09-28 12:06:18
Witam worker_thread źle wyświetla wartości, gdzie zrobiłem błąd i jak go poprawić (chcę uruchamiać treda z klasy i przekazać do niego jakaś strukturę):

// sendmailthreads.cpp
C/C++
#include "sendemailthreads.h"

SendEmailThreads::SendEmailThreads( int id )
{
    thread_info tinfo;
    tinfo.client = id;
    tinfo.ctx = id;
    tinfo.th_id = id;
   
    // error -1, ok > 0
    int ret = pthread_create( & thread, NULL, & worker_thread, & tinfo );
   
    if( ret != 0 ) {
        cout << "Error: pthread_create() failed " << "\n ";
        exit( EXIT_FAILURE );
    } else {
        cout << "In main: creating thread " << " \n";
    }
   
    if( ret > 0 ) {
        fprintf( stderr, "Mam mowego klienta, pid=%d\n", ret );
    }
   
    if( ret < 0 ) {
        perror( "Unable to accept" );
        exit( EXIT_FAILURE );
    }
}

bool SendEmailThreads::SaveToFile( string filename, string text ) {
    try {
        // save to file
        std::ofstream outfile;
        mkdir( "files", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
        outfile.open( "files/" + filename + ".log", std::ios_base::app );
        outfile << text << endl;
        outfile.close();
        return 1;
    } catch( const std::exception & exc ) {
        return 0;
    }
}

// Doo this jobs in the loop
void * SendEmailThreads::worker_thread( void * arg )
{
    thread_info * tinfo =( thread_info * ) arg;
    while( 1 ) {
        // Send emails from DATABASE
       
        // Save to file logs
        time_t seconds = time( NULL );
        SaveToFile( "thread", "Thread " + std::to_string(( int ) tinfo->client ) + " time " + std::to_string(( int ) seconds ) );
        // micro seconds 1s = 1000000
        usleep( 1000000 );
    }
    pthread_exit( NULL );
}
// send to thread data struct
struct SendEmailThreads::thread_info;
// new thread
pthread_t SendEmailThreads::thread;

sendmailthreads.h

C/C++
#ifndef SENDEMAILTHREADS_H
#define SENDEMAILTHREADS_H

// std::string
#include <string>
// pthread
#include <pthread.h>
// time
#include <time.h>
// mkdir
#include <sys/stat.h>
// usleep
#include <unistd.h>
//streaf
#include <fstream>
// cin, cout
#include <iostream>

using namespace std;

class SendEmailThreads
{
public:
    // Constructor
    SendEmailThreads( int id );
    // new thread
    static pthread_t thread;
    // Save to file log
    static bool SaveToFile( string filename, string text );
    // used for worker thread function
    static void * worker_thread( void * arg );
    // Send to thread struct
    struct thread_info {
        // pthread_t client;
        // SSL       *ssl;
        int th_id;
        int ctx;
        int client;
    };
};

#endif // SENDEMAILTHREADS_H

Uruchamiam:
SendEmailThreads t1 = SendEmailThreads(5);

Pozdrawiam
P-165291
Monika90
» 2017-09-28 12:31:28
tinfo to zmienna lokalna więc po wyjściu z konstruktora nie istnieje, a wątek próbuje jej używać przez wskaźnik.

Najlepiej użyj C++.
P-165292
Breakermind
Temat założony przez niniejszego użytkownika
» 2017-09-28 17:15:24
Dzięki, faktycznie nie dodałem deklaracji w klasie a tylko strukturę :).
To tak jak się za dużo rzeczy na raz robi :D. Tak właśnie czułem że to Monika90 napisze ;)
Pozdrawiam :x
P-165296
« 1 »
  Strona 1 z 1