简体   繁体   English

在线程中设置全局变量

[英]Setting Global variables in thread

I need to have a string as a global variable. 我需要一个字符串作为全局变量。 There is a possibility for multiple threads to set the global variable. 多个线程可以设置全局变量。 Should I have to go for mutex for this? 我应该为此寻求互斥吗? Or will OS handle such actions. 或OS将处理此类操作。 Going for mutex affects the application performance. 使用互斥锁会影响应用程序性能。

I am not concerned about the order of actions happening. 我并不担心行动的顺序。 I am afraid of the data corruption. 我害怕数据损坏。 Could somebody let me know about this. 有人可以让我知道这个。

It sounds like you understand all of the concerns. 听起来你理解所有的担忧。 If the global variable can be corrupt you definitely need to lock it in a mutex. 如果全局变量可能已损坏,则肯定需要将其锁定在互斥锁中。 This will affect performance, since this part is by definition now going to be synchronous. 这将影响性能,因为根据定义,这部分现在将是同步的。 That being said, you will want to lock the smallest part of the code as necessary, to minimize the time that synchronous code is being called. 话虽这么说,您将希望根据需要锁定代码的最小部分,以最大限度地减少调用同步代码的时间。

What's your global variable? 你的全局变量是什么? A pointer to the string buffer, or the buffer itself? 指向字符串缓冲区或缓冲区本身的指针?

On many architectures (including AFAIR 32-bit x86) overwriting a single pointer is atomic. 在许多体系结构(包括AFAIR 32位x86)上,覆盖单个指针是原子的。

This example might work: 此示例可能有效:

volatile char **global_var;

void set_var(char *str) {
    char *tmp = strdup(str);
    global_var = &tmp;
}

You can use Thread-Local Storage for this. 您可以使用线程局部存储
Unfortunately, its not specified in current C99 standart, but possible will be there in C1X. 不幸的是,它没有在当前的C99标准中指定,但可能会在C1X中出现。 For now, you can use compiler-specific implementations (GCC, ICC and Visual C have it). 目前,您可以使用特定于编译器的实现(GCC,ICC和Visual C都有)。

As far as the standards are concerned, yes, you must use a mutex. 就标准而言,是的,您必须使用互斥锁。 Failure to do so results in undefined behavior. 如果不这样做会导致未定义的行为。 In practice, most machine architectures will have no problem with this. 实际上,大多数机器架构都没有问题。 Future versions of the C standard (C1x) will have atomic types which, if used here, would definitely make the assignment without lock safe (albeit possibly using an internal lock, on broken archs that lack real atomics). C标准(C1x)的未来版本将具有原子类型,如果在这里使用,肯定会使得没有锁定的分配安全(尽管可能使用内部锁定,在缺乏真实原子的破坏的拱门上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式设置全局环境变量 - Setting global environment variables programmatically 在C线程编程中未正确获取全局变量 - Global variables not correctly acquired in C thread programming 检查全局变量线程安全保护的工具 - Tool to check global variables thread-safe protection CGO 块中的全局变量 - memory 泄漏和 GO 中的线程安全 - Global variables in CGO block - memory leak and thread safety in GO 如何使C中不同类线程中包含的全局变量安全 - How to make global variables that are contained in a different class thread safe in c 如何将全局变量相邻放置? (没有在 linker 脚本中设置它) - how to place global variables placed adjacent? (without setting it at linker script) 如何链接非线程安全库,以便每个线程都有自己的全局变量? - How to link non thread-safe library so each thread will have its own global variables from it? 全局变量到局部变量 - Global variables to local variables C:在不使用全局变量的情况下,从创建的线程中处理main中声明的结构 - C: Manipulate a structure declaired in the main from a created thread without the use of global variables 全局变量未在线程中更改? - Global variable not changing in thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM