?
Solved

pthread sync

Posted on 2001-07-19
8
Medium Priority
?
875 Views
Last Modified: 2008-01-09
I have an application that is getting bigger and bigger and application threads are running wild.  someone told me to use semaphore to syncronize them.  
for instance,  i have thread1, thread2, thread3, thread4, ..... thread22.
i want thread2 to wait on 1 and I want thread 3 to wait on thread2. I want threads 15-22 to wait on thread7 , then thread8 wait on thread22   got the picture?, why make them threads, because each one is responsible for a specific HW.
my questions are:
-->could I use semaphore for this, or is there a better way
------->if semaphore will do the job, is there somewhere i can learn they quick, how-to, tutorial, examples, etc etc?
-------> if ! what can I use?

ps.  the order of threads might change, I could stop thread2, then thread3 will wait for thread1.
0
Comment
Question by:ragaMuffin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 6300630
If you're keeping track of the thread id's, you might want to try pthread_join.
0
 

Author Comment

by:ragaMuffin
ID: 6305521
that is not what I'm looking for
0
 

Author Comment

by:ragaMuffin
ID: 6305525
It should be like an interrupt, each device, or thread get a slice of the 20 mils, so all threads must run once, during an interval of 20 milliseconds
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:ragaMuffin
ID: 6305526
and pthread_join, waits until the thread exits, my threads are running still.  so you see I'm looking for a scheduler.
sorry for the multiple comments.
0
 
LVL 2

Expert Comment

by:nahumd
ID: 6305785
Yes you can use semaphore (mutex) to synchronize the threads as you wanted.
Use pthread_mutex_init to create the mutex, mutex_lock and mutex_unlock to signal and wait for a critical section.
0
 

Author Comment

by:ragaMuffin
ID: 6306282
nahumd,
can you point me somewhere where there is an example or a how-to document, other than man pages?
0
 
LVL 5

Expert Comment

by:garboua
ID: 6306293
/* This example is copied from */
/* http://www.coe.uncc.edu/~abw/parallel/pthreads/pthreads.html */
/* It illustrates the idea of shared memory programming */

/* Comments: the behavior of this program on Solaris 2.7 is quite different from that of Red Hat Linux 6.2. On Solaris, the program acted incorrectly while the outcome on Red Hat Linux 6.2 was as expected. X.M. */

#include <pthread.h>  
#include <stdio.h>
#define MAX 10
#define MAX_COUNT 15

void * increment(int *id);
void * watch(int *id);

int count =0;
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t count_max = PTHREAD_COND_INITIALIZER;
int thread_id[3]  = {0,1,2};

main()
{
        int i;
        /* define the type to be pthread */
       
        pthread_t thread[3];
        /* create 3 threads*/
        pthread_create(&thread[2], NULL, (void *)watch, &thread_id[2]);
        pthread_create(&thread[0], NULL, (void *)increment, &thread_id[0]);
        pthread_create(&thread[1], NULL, (void *)increment, &thread_id[1]);
       
        for(i=0; i< 3 ; i++)
        {
                pthread_join(thread[i], NULL);
        }
}

void * watch(int *id)
{
        /* lock the variable */
        pthread_mutex_lock(&count_mutex);
        while(count <= MAX_COUNT)
        {
                /* using the condition variable for waiting for the event */
                pthread_cond_wait(&count_max, &count_mutex);
                printf("Inside the watch() and the value is %d\n", count);
          fflush(stdout);
        }
        /*unlock the variable*/
        pthread_mutex_unlock(&count_mutex);
}

void * increment(int *id)
{
     int i;
     for(i=0; i< MAX ; i++)
     {
          /* lock the variable */
          pthread_mutex_lock(&count_mutex);
          count++;
          printf("in increment counter by threadof id :%d, and count: %d\n",*id, count);
       fflush(stdout);
          /* for the condition notify the thread */
          pthread_cond_signal(&count_max);
          /*unlock the variable*/
          pthread_mutex_unlock(&count_mutex);
          sleep(rand()%2);
     }
}
This is give you an idea on how to use mutex
0
 
LVL 5

Accepted Solution

by:
garboua earned 400 total points
ID: 6306303
for more illustration and full documentation, read the C programming mini course sections about semaphores and pthread.

threads:
http://docs.linux.cz/c_marshall/node29.html#SECTION002900000000000000000
IPC SEMAPHORE
http://docs.linux.cz/c_marshall/node26.html#SECTION002600000000000000000
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question