LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2004, 09:07 PM   #1
aenasir
LQ Newbie
 
Registered: Oct 2004
Posts: 1

Rep: Reputation: 0
semaphores and threads


Hi everyone,

I am writing a very simple multithreaded program. I am using semaphores to synchronize the threads. The program behaves similar with or without sem_wait and sem_post; i.e., even if I do not use sem_wait and sem_post the threads synchronize correctly and I do not observe any race condition between the threads in incrementing the counter. I appreciate if you inform me of your findings in this piece of code.

I compile the program as follows:

gcc -lpthread myprog.c

Here is the code of my program:


struct idList {
pthread_t thID;
struct idList *next;
};


long counter = 0; /* variable to be incremented by each thread */
sem_t mysemaphore; /* global semaphore */


/* This function specifies the body of each thread */

void *some_thread( void *param)

{
int j , temp;
int id = *(int *)param;

free( param);

for( j = 0; j < UPPER; j++ ) {

sem_wait( &mysemaphore ); /* semaphore WAIT operation */
counter = counter + 1;

sem_post( &mysemaphore ); /* semaphore SIGNAL operation */

}
pthread_exit( NULL );
}





main( int argc, char *argv[] )
{


....

pthread_t *threadID; // The ID of the newly created thread



/* Initialize the mutual exclusion semaphore */
sem_init( &mysemaphore, 0, n);
struct idList *root; // Points to a linked list of thread IDs
struct idList *currNode, *temp;

/* Create n identical threads */

for( i = 0; i < n; i++ ) {
param = (int *)malloc(sizeof(int));
*param = i; /* SEPARATE copy of its parameter */

temp = (struct idList *) malloc(sizeof(struct idList));


if (!i) { root = temp; currNode = temp; }

threadID = (pthread_t *)malloc(sizeof(pthread_t));

int thCrEr = pthread_create( threadID , NULL, some_thread, param);

if ( thCrEr )
printf("Error in creating a new thread!!!");



temp->thID = *threadID;
temp->next = NULL;
currNode->next = temp;
currNode = temp;

printf("A new thread created with ID %d \n", *threadID);
}


currNode = root;

/* Wait for all the threads to exit */

for( i = 0; i < n; i++ ) {
pthread_join( currNode->thID, NULL);
currNode = currNode->next;

}


/* Display final contents of counter */
printf( "\nFinal value of counter: %d", counter );
printf( " (should be %d)\n", n*UPPER );
exit(0);

}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
diff. between semaphores/mutex usage on threads skywalker27182 Programming 6 08-16-2004 11:34 PM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
Semaphores help mojozoox Programming 1 01-07-2004 08:41 AM
semaphores charles_sjc Linux - Software 2 12-06-2003 12:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration