Du solltest dir nochmal ein Grundlagenbuch zu C (nicht C++!) schnappen.
Die Speicherklasse "static" bewirkt mitnichten, dass eine Variable "immer mit gleichen Parametern verwendet wird" -- kein Ahnung, was das heißen soll. "static" bedeutet, dass das Symbol außerhalb des Blockes, in dem es definiert wird, unsichtbar ist. Das ist bei funktionslokalen Variablen zwangsläufig der Fall, weil diese mit dem Stackframe der Funktion nach deren Ende ja wieder ungültig werden.
Sinnvoll ist "static" dagegen außerhalb von Blöcken. Ein als "static" markiertes Symbol ist außerhalb des Quellcodefiles unsichtbar.
Zu deinem Problem -- folgender Code funzt:
Code: Select all
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_attr_t attr;
void *thread_function(void *arg)
{
/* Sleep 1000s */
sleep(1000);
/* This point should never be reached before the process is terminated...*/
fprintf(stderr,"Reached a point that should never be reached!");
}
void startThread(void* func(void*), void *arg)
{
pthread_t *thread;
int r;
if ((thread=(pthread_t*)malloc(sizeof(pthread_t)))==NULL) {
perror("startThread, malloc");
exit(EXIT_FAILURE);
}
if (pthread_create(thread,&attr,func,arg)==-1) {
perror("startThread, pthread_create");
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[])
{
int i;
if (pthread_attr_init(&attr)==-1) {
perror("main, pthread_attr_init");
exit(EXIT_FAILURE);
}
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)==-1) {
perror("main, pthread_attr_setdetachstate");
exit(EXIT_FAILURE);
}
for(i=0;i<1000;i++) {
startThread(thread_function,NULL);
fprintf(stdout,"%d\n",i);
}
/* Wait 500s to let the user inspect the example. */
sleep(500);
if (pthread_attr_destroy(&attr)==-1) {
perror("main, pthread_attr_destroy");
exit(EXIT_FAILURE);
}
}
Und das Makefile dazu:
Code: Select all
.PHONY: all clean
all: thread
clean:
-rm thread *.o
thread: thread.o
$(CC) -lpthread -o $@ $^
Janka
Ich vertonne Spam immer in /dev/dsp statt /dev/null.
Ich mag die Schreie.