c - 这个函数是完全线程安全的吗?

标签 c multithreading thread-safety pthreads

EDIT3:每次需要时它都会在一个新线程中产生,“输入”是一个在其中释放的 char* 的副本。假设 cURL 函数是线程安全的。

EDIT4:假设任何不可见的函数都是线程安全的。

static void *Com_GoogleTranslate(void* input) {

 CURL *easy_handle;
 char *pos1, *pos2, url[1024], final[1024], inlang[8], outlang[8], *encoded;
 const int const_strlen = strlen("\"translatedText\":\"");
 struct GoogleMem chunk;

 pthread_mutex_lock( &GoogleMessage_mutex );
  // 'auto' is really empty in google API:
  if (!strcmp(clu.translateIn->string, "auto"))  
   strcpy(inlang, "");
  else            
   strcpy(inlang, clu.translateIn->string);

  if (!strcmp(clu.translateOut->string, "auto")) 
   strcpy(outlang, "");
  else            
   strcpy(outlang, clu.translateOut->string);
 pthread_mutex_unlock( &GoogleMessage_mutex );

 // Build the URL
 url[0] = '\0';
 strcat(url, "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=");

 // Encode input into URL formatting
 encoded = url_encode((char*)input);
 if (!encoded) return 0;

 strcat(url, encoded);
 strcat(url, "&langpair=");
 strcat(url, inlang);
 strcat(url, "|");
 strcat(url, outlang);

 chunk.memory = malloc(1);  // realloc grows it at Com_GoogleTranslateMem()
 if (!chunk.memory) return 0;
 chunk.size = 0;    // no data yet

 // cURL initialization for this sub-session:
 easy_handle = qcurl_easy_init(); 

 // ioq3-urt: was needed on https:// (v2 API) attempts when using GnuTLS
 //qcurl_easy_setopt(curl_handle, CURLOPT_SSLVERSION, 3); 

 // set URL:
 qcurl_easy_setopt(easy_handle, CURLOPT_URL, url);

 // ioq3-urt: required for multithreading according to cURL doc.
 qcurl_easy_setopt(easy_handle, CURLOPT_NOSIGNAL, 1);

 // ioq3-urt: skip peer verification; required for google translate when SSL was used
 //qcurl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);

 // send all data to this function
 qcurl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, Com_GoogleTranslateMem);

 // we pass our 'chunk' struct to the callback function:
 qcurl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, (void *)&chunk);

 // some servers don't like requests that are made without a user-agent field, so we provide one:
 qcurl_easy_setopt(easy_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

 // ioq3-urt: Required by Google Translate terms:
 qcurl_easy_setopt(easy_handle, CURLOPT_REFERER, "ioq3-urt");

 // fetch it
 qcurl_easy_perform(easy_handle);

 // cleanup curl stuff
 qcurl_easy_cleanup(easy_handle);

 /*
  Now chunk.memory points to a memory block that is chunk.size
  bytes big and contains the remote file.

  Nothing has yet deallocated that data, hence free() is used at the end.
 */

 if (!chunk.size) {
  pthread_mutex_lock( &GoogleMessage_mutex );
   sprintf(GoogleMessage.message, "Translation: no data received from Google\n");
   GoogleMessage.new_message = qtrue;
  pthread_mutex_unlock( &GoogleMessage_mutex );
  // Free up memory (same with the end)
  if(chunk.memory) free(chunk.memory);
  free(encoded); 
  free(input);
  return 0;
 }

 if ( ( pos1 = strstr(chunk.memory, "\"translatedText\":\"") ) ) {  // Make sure we use a valid file:

  pos2 = strstr(pos1 + const_strlen, "\""); // position translated text ends

  // Build the translated text:
  final[0] = '\0';
  strncat(final, pos1 + const_strlen, strlen(pos1) - ( strlen(pos2) + const_strlen ) );

  // Final printing of the translated text:
  pthread_mutex_lock( &GoogleMessage_mutex );
   sprintf(GoogleMessage.message, "^2Translated^7: ^3%s\n", final);
   GoogleMessage.new_message = qtrue;
  pthread_mutex_unlock( &GoogleMessage_mutex );    
  #ifdef BUILD_FREETYPE
   TTF_Find_Slot(final, clu.TTF_MessageMaxTime->integer); 
  #endif

 } else {
  pthread_mutex_lock( &GoogleMessage_mutex );
   sprintf(GoogleMessage.message, "Translation: no valid translation file received from Google\n");
   GoogleMessage.new_message = qtrue;
  pthread_mutex_unlock( &GoogleMessage_mutex );  
 }

 // Free allocated memory
 if(chunk.memory) free(chunk.memory);
 free(encoded);
 free(input);
 return 0;
}

我只在某些系统上遇到不稳定的行为,虽然我怀疑它的硬件有问题(Intel OpenGL?),但我想知道我是否遗漏了什么。

编辑:假设 cURL 本身是线程安全的。

EDIT2:“输入”是释放到该线程中的新副本。

最佳答案

只要它在互斥锁保护部分之外调用的所有函数本身都是线程安全的,它对我来说就看起来是线程安全的。

然而,malloc 的线程安全性存在问题。参见 this question .

关于c - 这个函数是完全线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4365220/

相关文章:

c++ - 将静态 C 库与 C++ 代码链接时出现 "undefined reference to"错误

java - xml 解析在 C/C++ 或 Java 中更快?

c++ - 结构/类位域打包

multithreading - 为什么内核线程在进程上下文中运行?

c++ - 在 C++11 中将原子与 std::thread 一起使用

c# - 与 SQLite Dapper 的多个连接

multithreading - 在可能出现异常(exception)情况的将来应用 future

java - 调用notifyAll时出现IllegalMonitorStateException

动态创建的文本框上的 C# 线程安全文本更新

C - 将代码分解成单独的文件