R
R
Roman Rakzin2015-08-07 02:39:51
go
Roman Rakzin, 2015-08-07 02:39:51

Dealing with pointers in golang and locks?

Good afternoon.
I have a reference variable var MaxData *int which is incremented each time.

func new_data(){
    *MaxData = *MaxData +1
    internal_max_data=*MaxData // вообщем как-то хочу внутренней переменной присвоить значение MaxData и потом работать в функции с internal_max_data 
}

In fact, many new_data() functions can work with *MaxData in goroutines,
and this is the question that arises. Theoretically, it can be assumed that one goroutine increased MaxData, and then the second one also increased, and then the first one assigned internal_max_data MaxData, which was already increased by 2 ... and the second goroutine at that time also took the same value.
Well, judging from this, the program will blurt out. In general, somehow it is necessary to block MaxData for this period of time. And in general - how to work with pointers? and then I have erors everywhere.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey K, 2015-08-07
@TwoRS

golang.org/pkg/sync/atomic

S
SilentFl, 2015-08-07
@SilentFl

Ha ha, Roma _suddenly_ realized that he was writing competitive code7a31d6fa6fde4a01be658a734845aecc.gif

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question