Technical help with pthread_create_key and other key calls

Brian Budge brian.budge@gmail.com
Sun Sep 17 05:39:00 GMT 2006


Hi Jen -

This is not really a gcc related question.  You might try a forum like
the ones at www.unix.com

  Brian

On 9/16/06, Mead, Jennifer L - VSCM <Mead.Jennifer@vectorscm.com> wrote:
>
>
> -----Original Message-----
> From: Mead, Jennifer L - VSCM
> Sent: Friday, September 15, 2006 4:03 PM
> To: 'gcc-help@gcc.gnu.org'
> Subject: Technical help with pthread_create_key and other key calls
>
>
> Hi C folks,
>
> I am just getting back into C from a 15 hiatus, oh how I missed it.  Now
> I am trying to write a multi-threaded application that is very very
> simple but somehow I cannot make the pthread_setspecific key calls work
> for me.  I have tried malloc-ing space, and moving all the code calls
> everywhere but loose.  I think I need help.  Below is a skeleton of what
> I am trying to do.  Can someone tell me how to fix this?  Mucho
> appreciato.
>
> Background: this C program is redirected in a file with directory names,
> I am trying to spin a thread for every directory in the file.  There are
> over 200 threads I need to start.  I don't seem to have a problem doing
> that, but the key that I send at the create statement gets written over
> everytime and then all the previous threads are looking at the same
> directory.
>
>
> 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);
>
>
>
> Regards,
> Jen
>
> Jennifer L Mead
> ICC Operations Sr Sys Engineer
> Con-way Enterprise Services
> 503.450.8578            desk
> 503.550.6589            cell
> mead.jennifer@vectorscm.com
>
>
>



More information about the Gcc-help mailing list