- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的第一个 pthread 程序,我不知道为什么 printf 语句在子线程中打印两次:
int x = 1;
void *func(void *p)
{
x = x + 1;
printf("tid %ld: x is %d\n", pthread_self(), x);
return NULL;
}
int main(void)
{
pthread_t tid;
pthread_create(&tid, NULL, func, NULL);
printf("main thread: %ld\n", pthread_self());
func(NULL);
}
在我的平台(Linux 3.2.0-32-generic#51-Ubuntu SMP x86_64 GNU/Linux)上观察到的输出:
1.
main thread: 140144423188224
tid 140144423188224: x is 2
2.
main thread: 140144423188224
tid 140144423188224: x is 3
3.
main thread: 139716926285568
tid 139716926285568: x is 2
tid 139716918028032: x is 3
tid 139716918028032: x is 3
4.
main thread: 139923881056000
tid 139923881056000: x is 3
tid 139923872798464tid 139923872798464: x is 2
对于 3,来自子线程的两个输出行
对于 4,与 3 相同,甚至输出也是交错的。
最佳答案
线程通常通过时分复用发生。处理器在两个线程之间均匀切换通常效率较低,因为这需要更多的努力和更高的上下文切换。通常,您会发现线程在切换之前会执行多次(如示例 3 和 4 的情况)。子线程在最终终止之前会执行多次(因为主线程已退出)。
Example 2: I don't know why x is increased by the child thread while there is no output.
考虑一下这一点。主线程执行。它调用 pthread 并创建一个新线程。新的子线程递增 x。在子线程能够完成 printf 语句之前,主线程启动。突然它也增加了 x。然而,主线程也能够运行 printf 语句。突然 x 现在等于 3。主线程现在终止(也导致子线程 3 退出)。这很可能就是您的案例中发生的情况,例如示例 2。
示例 3 清楚地表明,由于锁定效率低下和堆栈数据损坏,变量 x 已被损坏!!
有关什么是线程的更多信息。
Link 1 - Additional info about threading
Link 2 - Additional info about threading
您还会发现,由于您使用的是 x 的全局变量,因此对该变量的访问在线程之间共享。这很糟糕。非常非常糟糕,因为访问同一变量的线程会由于变量 x 的同一寄存器上发生多次读写而创建竞争条件和数据损坏。正是由于这个原因,使用互斥锁本质上是在更新变量时创建一个锁,以防止多个线程同时尝试修改同一变量。互斥锁将确保 x 按顺序更新,而不是像您的情况那样偶尔更新。
有关常规 Pthread 和互斥锁定示例的更多信息,请参阅此链接。
Pthreads and Mutex variables
干杯,
彼得
关于pthreads - 线程: one printf statement get printed twice in child thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809727/
我不知道该在标题中添加什么内容,但我有以下代码: @Controller public class WorkdayAddController { @Autowired private WorkdayR
已编辑:我需要用户按其全局进度 [order by avg(progression) DESC] 以及按进度排序的详细信息行进行排序。 示例: USER 1 campaign 1 progressio
我知道这是一个更“沉重”的问题,但我认为它也很有趣。它是 of my previous questions about compiler functions 的一部分, 但回过头来我解释得非常糟糕,许
我有一个函数(表单中的事件处理程序)结构如下: Dim errMsg as String = "" CheckIfValidUser(..., errMsg) If errMsg.Length > 0
我正在为客户正在举办的事件创建注册表。基本的用户详细信息被提交到第三方系统(即姓名、电子邮件等),但其余字段需要通过电子邮件发送给客户。 我需要这样的事情发生: 列出项目 用户填写表单 jquery
我有一个ClassLoader,它从源文件加载由JavaCompiler编译的类。但是当我更改源文件、保存它并重新编译它时,ClassLoader 仍然加载该类的第一个版本。 ClassLoad
我有一个使用 Kotlin 编写的 RecognitionListener 的项目。语音转文本功能一直很成功,从未出现任何问题。 从上周开始,它的 onResult 函数开始被调用两次。该项目没有进行
大家好(和圣诞快乐) 希望有人能回答我的问题,那将是一个美好的圣诞节 礼物。 我的 UIViewController 一个表,在表部分区域中有一个分段控件。看起来像这样 每次用户单击分段控件时,我都会
我正在制作一个程序,输入三个数字,然后计算一些不同的东西(每个东西都必须有自己的函数)。该程序首先告诉用户他们的选择并等待他们的输入。执行任何情况后,程序将再次打印菜单,除非它将使用默认情况,然后它将
我的 Android 应用程序有一个自定义接收器,如下所示: public class CustomReceiver extends BroadcastReceiver{ String URL; St
我有一系列带日期的事件,如下所示: 事件 1,2014 年 1 月 1 日 事件 2,2014 年 1 月 2 日 事件 3,2014 年 1 月 3 日 假设我想要按时间顺序排列的最近两个事件。如果
我对 jQuery“切换”方法有一个神秘的问题。我单击它一次,但它会设置两次动画。你自己去看看吧! Demo 页面源码贴在下面,(解决后链接会消失) html: About us S
我对Python完全陌生,当我遇到这个问题时,我正在尝试线程模块:-线程由于某种原因运行两次,我不知道为什么。我到处寻找,但没有找到任何答案。希望我能在这里得到一些帮助 import time fro
先决条件:无障碍对讲开启。 问题:当从软键盘向编辑文本输入字符时,字符被读出两次。(我想一次是通过键盘,一次是通过编辑文本)。 最佳答案 问题:视力正常的用户不能很好地理解/理解盲人用户需要的信息类型
我的问题是,当我再次对相同的值进行评分时,它不会响应第二次..这意味着当我第一次评分为“4”时,我不能将其评分为“4”第二次..只有当我评价其他值而不是'4'时它才会响应。 这是我尝试过的(我的代码中
我必须设计一个问卷系统。我们一直在讨论很多事情,包括如何持有答案,以及如何持有可能的答案。现在,尽管我不同意,但我还是屈服于我的同事。我只想知道您对此的看法以及原因。对我来说,两次保存相同的信息是一个
我将 Angular2 与路由一起使用,我的页面在初始加载时显示重复内容。单击任何链接(Page1 或 Page2)后,一切正常(不再有重复内容)。为什么我在初始加载时得到重复的内容以及我需要什么才能
我用KTX。。我有以下代码:。为什么它显示了两次?我该如何解决这个问题呢?。。预计“点击开始游戏”的标签只会显示一次。。我试着删除了代码。但它还是发生了。。然后,我尝试删除Add(LoadingSta
现象说明 maven的java项目,测试用例和main所在的源码文件均符合缺省写法和格式,但是在使用mvn clean sonar:sonar进行编译时提示can't be indexed twi
我使用 React 创建了一个简单的 Meteor 应用程序。它使用名为 client 的文件夹中的三个文件(没有其他文件)。在控制台中,应用程序打印出: withTracker rendering
我是一名优秀的程序员,十分优秀!