panjf2000 / ants

🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.
https://ants.andypan.me/
MIT License
12.83k stars 1.36k forks source link

[Question]: can you please confirm the proper way for variables to be sent using ants? #271

Closed kolinfluence closed 1 year ago

kolinfluence commented 1 year ago

Questions with details

    ipAddrCopy := make([]byte, len(ipAddr.AsSlice()))
    copy(ipAddrCopy, ipAddr.AsSlice())
    ants.Submit(func() {
        cRedis.Do("PUBLISH", "b", "k"+cx.B2s(ipAddrCopy))
    })

is this ok? how do i ensure ipAddrSend is correctly send without the gc clearing the value? i wanted to do this directly but i worried ipAddr will be nil if it's not created new. What's your suggestion?

        ants.Submit(func() {
                cRedis.Do("PUBLISH", "b", "k"+ipAddr.String())
        }

Code snippets (optional)

No response

panjf2000 commented 1 year ago

is this ok? how do i ensure ipAddrSend is correctly send without the gc clearing the value?

What ipAddrSend? you meant ipAddrCopy? if so, it won't be released by GC before your task is done.

i wanted to do this directly but i worried ipAddr will be nil if it's not created new. What's your suggestion?

Can't you just check if ipAddr is nil before cRedis.Do?