I have a problem with a C multi-threaded program I am writing. I cannot figure out how to keep the unique key value at the thread level. I wrote a program in C that forked a bunch of processes and then decided to convert it to threads and I can't keep the key unique to each thread. In a nutshell it reads a file with a valid pathname on every line (approx 200) and starts a thread to watch over a unique directory (simple polling program). Error is when I create a new thread, then every pre-exisiting thread is looking at the last pathname read in. So I end up with 200+ threads watching the same directory.
Following is what I think is a big enough snippet from main and the pthread_create sub to give something for someone to point out what I am doing wrong. Thanks in advance, Jennifer
> MAIN
> --------
> pthread_t main_thr;
> pthread_t thr;
> pthread_attr_t attr;
>
> main_thr = pthread_self();
> p=malloc(2);
> p = buffer;void *watch_dir(void *arg)
>
> pthread_key_t key;
>
> pthread_key_create(&key, NULL);
> thread_setspecific(key, arg);
> arg=pthread_getspecific(key);
> pthread_attr_init(&attr);
> pthread_attr_setdetachstate(&attr, 1);
>
> if (pthread_create(&thr, &attr, watch_dir, p))
> {
> fprintf(fplog, "Can't create thr\n"), exit(1);
> }
>
> WATCH_DIR
> ------------------
> void *watch_dir(void *arg)
> {
>
> FILE *fplog;
> time_t curtime;
> struct tm *loctime;
> DIR *dir_p;
> struct dirent *dir_entry_p;
> pthread_key_t key;
> pthread_key_create(&key, NULL);
> pthread_setspecific(key, arg);
> arg=pthread_getspecific(key);
>
> for (;
> {
> fplog=fopen(FTP_LOG, "a");
>
> curtime = time (NULL); /* get current time */
> loctime = localtime (&curtime);
> fputs (asctime (loctime), fplog);
> sleep(3);
>
>
> dir_p = opendir(arg);
> while( NULL != (dir_entry_p = readdir(dir_p)))
> {
> if (!((strcmp(dir_entry_p->d_name, ".") == 0) ||
> (strcmp(dir_entry_p->d_name, "..") == 0)))
> {
> fprintf(fplog," %s \n", dir_entry_p->d_name);
> /*
> holder for FTP Control PERL script
> send: arg & dir_entry_p->d_name
> directory & filename
> */
> }
> /*
> printf("p is %s\n",p);
>