Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6891627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:26:02+00:00 2026-05-27T06:26:02+00:00

In my code there are three concurrent routines. I try to give a brief

  • 0

In my code there are three concurrent routines. I try to give a brief overview of my code,

Routine 1 {
do something

*Send int to Routine 2
Send int to Routine 3
Print Something
Print Something*

do something
}

Routine 2 {
do something

*Send int to Routine 1
Send int to Routine 3
Print Something
Print Something*

do something
}

Routine 3 {
do something

*Send int to Routine 1
Send int to Routine 2
Print Something
Print Something*

do something
}

main {
routine1
routine2
routine3
}

I want that, while codes between two do something (codes between two star marks) is executing, flow of control must not go to other goroutines. For example, when routine1 is executing the events between two stars (sending and printing events), routine 2 and 3 must be blocked (means flow of execution does not pass to routine 2 or 3 from routine 1). After completing last print event, flow of execution may pass to routine 2 or 3. Can anybody help me by specifying, how can I achieve this ? Is it possible to implement above specification by WaitGroup ? Can anybody show me by giving a simple example how to implement above specified example by using WaitGroup. Thanks.

NB: I give two sending and two printing options, in fact there is lots of sending and printing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:26:02+00:00Added an answer on May 27, 2026 at 6:26 am

    If I have gotten it correctly, what you want is to prevent simultaneous execution of some part of each function and other functions. The following code does this: fmt.Println lines won’t happen as other routines are running. Here’s what happens: when execution gets to the print section, it waits until other routines end, if they are running, and while this print line is executing other routines don’t start and wait. I hope that is what you are looking for. Correct me if I’m wrong about this.

    package main
    
    import (
        "fmt"
        "rand"
        "sync"
    )
    
    var (
        mutex1, mutex2, mutex3 sync.Mutex
        wg sync.WaitGroup
    )
    
    func Routine1() {
        mutex1.Lock()
        // do something
        for i := 0; i < 200; i++ {
            mutex2.Lock()
            mutex3.Lock()
            fmt.Println("value of z")
            mutex2.Unlock()
            mutex3.Unlock()
        }
        // do something
        mutex1.Unlock()
        wg.Done()
    }
    
    func Routine2() {
        mutex2.Lock()
        // do something
        for i := 0; i < 200; i++ {
            mutex1.Lock()
            mutex3.Lock()
            fmt.Println("value of z")
            mutex1.Unlock()
            mutex3.Unlock()
        }
        // do something
        mutex2.Unlock()
        wg.Done()
    }
    
    func Routine3() {
        mutex3.Lock()
        // do something
        for i := 0; i < 200; i++ {
            mutex1.Lock()
            mutex2.Lock()
            fmt.Println("value of z")
            mutex1.Unlock()
            mutex2.Unlock()
        }
        // do something
        mutex3.Unlock()
        wg.Done()
    }
    
    func main() {
        wg.Add(3)
        go Routine1()
        go Routine2()
        Routine3()
        wg.Wait()
    }
    

    UPDATE: Let me explain these three mutex here: a mutex is, as documentation says: “a mutual exclusion lock.” That means when you call Lock on a mutex your code just waits there if somebody else have locked the same mutex. Right after you call Unlock that blocked code will be resumed.

    Here I put each function in its own mutex by locking a mutex at the beginning of the function and unlocking it the end. By this simple mechanism you can avoid running any part of code you want at the same time as those functions. For instance, everywhere you want to have a code that should not be running when Routine1 is running, simply lock mutex1 at the beginning of that code and unlock at at the end. That is what I did in appropriate lines in Routine2 and Routine3. Hope that clarifies things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code there are three concurrent routines. I try to give a brief
Are there any IDE's for developing HLSL code? The three key features I want
I want to write three concurrent routines that sends integer to each other. Now,
I want to write three concurrent go routines that sends integers to each other.
There is at least three well-known approaches for creating concurrent applications: Multithreading and memory
I have the following code string three() { return three; } void mutate(string& ref)
In the code there exists exactly one type that implements IResourceConverter. That's what the
In my code there are some calls to a method that set the values
In some existing code there is a test to see if the user is
Sorry if this question seems trivial to many here. In a C++ code there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.