Thread: synchroniz...

  1. #1
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187

    synchroniz...

    Is there any ways to make synchronization between 2 functions in C/C++? I leant from ADA language, there is a keyword `RENDEZVOUS' doing that.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You need to use the threading capabilities of your chosen os to simulate this functionality.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Can you elaborate what ``RENDEZVOUS'' does? (I tend to believe there aren't many ADA programmers reading the board)
    Something like...
    main() calls 2 functions simultaneously and both work independently when some value in those functions match together, both functions return to main().
    for realistic...
    In case aircraft will get landed...
    A function detects altitude. The other function detects speed.
    For sure both work simultaneously and independently. But before the aircraft gets landed, its altitude and speed must match together. If not, warning alarm bells.
    (I have an example of ADA using `rendezvous' But I don't code it here, I'm afraid that no one will read it and it might violate board regulations)
    Anyway I shall try POSIX pthread

    Thanks,
    Last edited by Jaguar; 03-18-2003 at 11:54 AM.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  4. #4
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    It still uses global variables.
    Anyway you gave much knowledge to me. I appreciated it.

    Thanks vVv,
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    If what I'm getting is that you want them to run many functions at the same time and then continue when all of them are finished then this should work fine:
    Code:
    void Function1(int &done)
    {
    	// stuff here...
    	done++;
    }
    
    void Function2(int &done)
    {
    	// stuff here...
    	done++;
    }
    
    int main()
    {
    	int done=0;
    
    	// you can have them both run in seperate threads
    	// or have just the first one in a new thread
    	// ...implementation specific...
    	Function1(done);
    	Function2(done);
    
    	// wait until they are both done
    	while (done!=2);
    
    	// continue here...
    	return 0;
    }
    If what you want is to lock a resource so that only one thread can use it at one time, it gets a little trickier. Use a little templated class to hold a boolean if its being used or not.
    Last edited by Speedy5; 03-18-2003 at 02:51 PM.

Popular pages Recent additions subscribe to a feed