-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
is curl_global_init handled safely? is the library thread-safe? #62
Comments
go-curl is not supposed to be thread safe. |
@andelf var curl_global_inited bool = false
func go_curl_global_init() {
var once sync.Once
onceBody := func() {
// call the real curl_global_init() here, and when done:
curl_global_inited = true
}
once.Do(onceBody)
for curl_global_inited != true {
// some thread is running curl_global_init, wait for it to finish.
// optimization notes, figure out if Go has built-in support for "wait until variable changes", and if 1 millisecond is a sane timeout?
time.Sleep(1 * time.Millisecond)
}
} and have all applicable functions call go_curl_global_init(), then go-curl should be thread-safe. as for performance, you'll have the function call overhead, but the time.Sleep code should only ever be hit in situations where you would actually have race condition without it, and will only run at the very first curl function calls, if ever, so i don't think it is a significant performance hit. also, i hope there's a way to make curl_global_inited a function-local static threads-shared variable, but i don't know. (ps, i'm not proficient with Go myself, only toyed with it) |
You can also use Example: https://github.com/umurkontaci/go-curl-transport/blob/master/transport/transport.go#L21-L23 |
@umurkontaci will that also help protect threads from calling curl_easy_init() before the thread calling curl_global_init() has completed the call? |
Possibly, If you import the module in the application entrypoint you would probably be okay, but it's quite hacky way to do. It'd be better to encapsulate everything together. |
actually, if you go with the sync.Once.Do method, you don't have to do the |
is the library supposed to be thread safe? note that curl_global_init() is NOT thread-safe, and curl_easy_init is threadsafe ONLY IF curl_global_init() has already been executed. if you start several threads that all call curl_easy_init() at the same time, before curl_global_init has been executed, you may run into problems, how does this reflect on go-curl?
The text was updated successfully, but these errors were encountered: