FAQ
Hi guys,

I'm implementing some toe algorithm and ran into following strange (as for
me) problem. I'm trying to implement multithreaded algorithm but I decided
to start with one thread before because there is at least one bug in this
algorithm. But in this single-threaded approach I'm trying to use
memoization goroutine and communicate with it through channels. But program
fails with deadlock because all goroutines are in sleep mode and nobody can
read from channel, but as I can see there should be at least one goroutine
running. It fails in get_mem function. Code is
here http://play.golang.org/p/AdmlZS-yHA .

Could somebody more confident with channels point me what is wrong in this
code?

Thanks,
Alex

--

Search Discussions

  • Alex at Dec 21, 2012 at 12:55 pm
    BTW, when I changed
    get.ch <- -1
    to
    get.ch <- 0

    It works OK which is strange.
    On Friday, December 21, 2012 3:52:48 PM UTC+3, Alex wrote:

    Hi guys,

    I'm implementing some toe algorithm and ran into following strange (as for
    me) problem. I'm trying to implement multithreaded algorithm but I decided
    to start with one thread before because there is at least one bug in this
    algorithm. But in this single-threaded approach I'm trying to use
    memoization goroutine and communicate with it through channels. But program
    fails with deadlock because all goroutines are in sleep mode and nobody can
    read from channel, but as I can see there should be at least one goroutine
    running. It fails in get_mem function. Code is here
    http://play.golang.org/p/AdmlZS-yHA .

    Could somebody more confident with channels point me what is wrong in this
    code?

    Thanks,
    Alex
    --
  • Alex at Dec 21, 2012 at 1:00 pm
    Heh. This is strange but after a small refactoring it works. I changed code
    to:
    case get := <-mem_get:
    res := -1
    if value, ok := mem[get.index]; ok {
    res = value
    }
    get.ch <- res

    is this a bug?

    http://play.golang.org/p/FpckXa-ORM

    Alex
    On Friday, December 21, 2012 3:52:48 PM UTC+3, Alex wrote:

    Hi guys,

    I'm implementing some toe algorithm and ran into following strange (as for
    me) problem. I'm trying to implement multithreaded algorithm but I decided
    to start with one thread before because there is at least one bug in this
    algorithm. But in this single-threaded approach I'm trying to use
    memoization goroutine and communicate with it through channels. But program
    fails with deadlock because all goroutines are in sleep mode and nobody can
    read from channel, but as I can see there should be at least one goroutine
    running. It fails in get_mem function. Code is here
    http://play.golang.org/p/AdmlZS-yHA .

    Could somebody more confident with channels point me what is wrong in this
    code?

    Thanks,
    Alex
    --
  • Jesse McNelis at Dec 21, 2012 at 1:42 pm

    On Sat, Dec 22, 2012 at 12:00 AM, Alex wrote:

    Heh. This is strange but after a small refactoring it works. I changed
    code to:
    case get := <-mem_get:
    res := -1
    if value, ok := mem[get.index]; ok {
    res = value
    }
    get.ch <- res

    The problem was that when there was a value in the mem map you sent two
    values to the channel, but only read one.

    case get := <-mem_get:
    if value, ok := mem[get.index]; ok {
    get.ch <- value //sent one value here
    }
    get.ch <- 0 //sent again here.


    The fix (it's not really a fix) you got by changing
    get.ch <- -1
    to
    get.ch <- 0

    is related to this section of code.
    if val := get_mem(index); val != -1 {
    return val
    }

    That is, because you return '0' you don't call calculate again and thus
    don't deadlock in get_mem() while trying to send a new channel to the
    already blocked(waiting to send the second value) memoaizer().

    --
  • Alex at Dec 21, 2012 at 2:01 pm
    Yeah, you are right. My bad.

    Thanks
    On Friday, December 21, 2012 4:42:27 PM UTC+3, Jesse McNelis wrote:

    On Sat, Dec 22, 2012 at 12:00 AM, Alex <alex....@gmail.com <javascript:>>wrote:
    Heh. This is strange but after a small refactoring it works. I changed
    code to:
    case get := <-mem_get:
    res := -1
    if value, ok := mem[get.index]; ok {
    res = value
    }
    get.ch <- res

    The problem was that when there was a value in the mem map you sent two
    values to the channel, but only read one.

    case get := <-mem_get:
    if value, ok := mem[get.index]; ok {
    get.ch <- value //sent one value here
    }
    get.ch <- 0 //sent again here.


    The fix (it's not really a fix) you got by changing
    get.ch <- -1
    to
    get.ch <- 0

    is related to this section of code.
    if val := get_mem(index); val != -1 {
    return val
    }

    That is, because you return '0' you don't call calculate again and thus
    don't deadlock in get_mem() while trying to send a new channel to the
    already blocked(waiting to send the second value) memoaizer().
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 21, '12 at 12:52p
activeDec 21, '12 at 2:01p
posts5
users2
websitegolang.org

2 users in discussion

Alex: 4 posts Jesse McNelis: 1 post

People

Translate

site design / logo © 2022 Grokbase