public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Lucas Prado Melo" <lukepadawan@gmail.com>
To: gcc@gcc.gnu.org
Subject: Re: Problem with posix threads
Date: Sun, 16 Dec 2007 20:00:00 -0000	[thread overview]
Message-ID: <9f4be2240712161144h49e5b3e8r20b673702727274d@mail.gmail.com> (raw)
In-Reply-To: <9f4be2240712161142r49e09784o9a3154901072d900@mail.gmail.com>

 Please forgive me if this is off-topic:
 I've written a simple test program with posix threads and a 'glibc'
attempt was detected.

 The code:


 -----main.c-------

 #include <stdio.h>
 #include <stdlib.h>
 #include <pthread.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <errno.h>
 #include <unistd.h>
 #include "stack.c"

 /*
  * THREAD EXPERIMENT
  *
  * There are various threads:
  * I, II and main
  * thread I repeatly pushes to 'stck' a value (NTIMES times)
  * thread II then pop repeatly and show the value (NTIMES-1 times)
  * main thread then pushes the last values and quit
  */
 #define NTIMES 10000000
 #define die(msg) do { perror(msg); exit(1); } while(0)

 void * doPush(void * data);
 void * doPop(void * data);

 struct pack{
     Stack stck;
     pthread_mutex_t mutex;
 };

 int main(int argc, char *argv[]){
     void *trashbin;
     pthread_t peer[2];
     struct pack pck;
     //initialize pck mutex
     if( pthread_mutex_init(&( pck.mutex), NULL) != 0 )
         die("pthread_mutex_init");

     //and make it multi-threaded
     if( pthread_create(&peer[0], NULL, doPush, (void*)&pck) != 0 )
         die("pthread_create");
     if( pthread_create(&peer[1], NULL, doPop, (void*)&pck) != 0 )
         die("pthread_create");
     //wait all threads do their stuff
     pthread_join(peer[0],&trashbin);
     pthread_join(peer[1],&trashbin);

     pthread_mutex_lock( &(pck.mutex) );
     printf("Last one: %c\n", pop( &(pck.stck) ));
     pthread_mutex_unlock( &(pck.mutex) );

     //destroy pck mutex
     if( pthread_mutex_destroy( &( pck.mutex) ) != 0 )
         die("pthread_mutex_destroy");
     return 0;
 }
 void * doPush(void * data){
     struct pack * pck = (struct pack *)data;
     int x;
     for(x=0;x<NTIMES;x++){
         int chr;
         chr = x%('z'-'a'+1);
         chr += 'a';
         //try to push the data... using mutex!
         pthread_mutex_lock( &(pck->mutex) );
         push( &(pck->stck), (void*)chr );
         pthread_mutex_unlock( &(pck->mutex) );
     }
     pthread_exit( NULL );
 }
 void * doPop(void * data){
     struct pack * pck = (struct pack *)data;
     int x;
     for(x=0;x<(NTIMES-1);x++){
         pthread_mutex_lock( &(pck->mutex) );
         printf("%c ", pop( &(pck->stck) ) );
         pthread_mutex_unlock( &(pck->mutex) );
     }
     printf("\n");
     pthread_exit( NULL );
 }


 ----------------------

 -----stack.c-------
 #ifndef STACK_C
 #define STACK_C 1
 #include <stdlib.h>


 struct stack {
     void * el;
     struct stack *next;
 };

 typedef struct stack * Stack;

 void stackInit(Stack * stck){
     *stck = NULL;
     return;
 }
 //pushes an element to the stack
 void push(Stack * stck, void * el){
     struct stack * ne;
     ne = malloc(sizeof(struct stack));
     ne->el = el;
     ne->next = *stck;
     *stck = ne;
     return;
 }

 //pops an element from stack
 //return NULL if there's no element in the stack
 void * pop(Stack * stck){
     struct stack * de;
     void * el;
     de = *stck;
     *stck = (*stck)->next;
     if(de != NULL ){
         el = de->el;
         free(de);
     }
     else
         el = NULL;
     return el;
 }

 #endif
 ----------------------

       reply	other threads:[~2007-12-16 19:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <9f4be2240712161142r49e09784o9a3154901072d900@mail.gmail.com>
2007-12-16 20:00 ` Lucas Prado Melo [this message]
2007-12-16 22:44   ` Lucas Prado Melo
2007-12-17  1:12     ` Daniel Jacobowitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9f4be2240712161144h49e5b3e8r20b673702727274d@mail.gmail.com \
    --to=lukepadawan@gmail.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).