0

After start a detach Thread, the thread go to sleep for a while. At the End of the sleep time, a variable was changed. But the variable only change the value in the thread. After exit of the thread the change don't exist any more.

That code run under Lam/MPI 7.4. Don't now if that is different to normal gcc.

typedef struct theadSleep {
    struct task *task;
    int sleeptime;
    int result;
} ThreadSleep;

void sleepTask(void *dummy) {
    static volatile ThreadSleep *tS;
    tS = (ThreadSleep*) dummy;
    time_t t1;
    t1 = time(NULL );
    int t1int = (int) t1;
    t1int = t1int + tS->sleeptime;
    while (t1int >= (int) time(NULL )) {
        sleep(1);
    }
    tS->task->result = tS->result;
    pthread_exit(NULL );
}


// Function to start the thread
pthread_attr_t attrSleep; /* Attribut für Posix Thread */
pthread_t sleepT; /* Posix Thread */
static volatile ThreadSleep ts1;
ts1.result = 0;
ts1.sleeptime = 0;
ts1.result = resultRecieved[0];
ts1.sleeptime = resultRecieved[1];
ts1.task = tmpTask;
pthread_attr_init(&attrSleep);
pthread_attr_setdetachstate(&attrSleep, PTHREAD_CREATE_DETACHED);
if (pthread_create(&sleepT, &attrSleep, &sleepTask, (void*) &ts1)   == -1) {
  fprintf(stderr, "Fehler bei Starten des Sleep Threads Task %i Kind %i.\n",
  tmpTask->taskindex, tmpTask->taskkind);
}

        }

Here the staement tS->task->result = tS->result; has no effect!

Example, but error free.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>

typedef struct task {
        int result;
        int sleeptime;
} Task;

typedef struct theadSleep {
        struct task *task;
        int sleeptime;
        int result;
} ThreadSleep;


void sleepTask(void *dummy) {
    ThreadSleep *tS;
    tS = (ThreadSleep*) dummy;
    time_t t1;

    t1 = time(NULL );
    int t1int = (int) t1;
    t1int = t1int + tS->sleeptime;
    while (t1int >= (int) time(NULL )) {
        sleep(1);
    }
    tS->task->result = tS->result;
    printf("Detach Thread is ready.\n");
    pthread_exit(NULL );
}

int main (){
  Task *newtask = malloc(sizeof(Task));
  newtask->result = 0;
  newtask->sleeptime = 3;

  pthread_attr_t attrSleep; /* Attribut für Posix Thread */
  pthread_t sleepT; /* Posix Thread */
  ThreadSleep ts1;
  ts1.result = 14;
  ts1.sleeptime = newtask->sleeptime;
  ts1.task = newtask;

  pthread_attr_init(&attrSleep);
  pthread_attr_setdetachstate(&attrSleep, PTHREAD_CREATE_DETACHED);
  if (pthread_create(&sleepT, &attrSleep, &sleepTask, (void*) &ts1)
                                        == -1) {
    fprintf(stderr, "Fehler bei Starten des Sleep Threads.\n");
  }
  sleep (5);

  printf("Task result: %i \n",newtask->result);

return 0;
}
4

2 に答える 2

0

memcpy(tS, dummy, sizeof(ThreadSleep));
また、ローカルtS = (ThreadSleep*) dummy;
でのみ変更を加えたい場合に適用されます。それ以外の場合は、memcpy と malloc を忘れてください。その場合、「ダミー」を ThreadSleep* にキャストするだけです。ただし、そのままにして、メモリリークを回避したい場合は、 も追加してfree();ください。

于 2013-06-19T19:12:32.457 に答える