码迷,mamicode.com
首页 > 其他好文 > 详细

pthread更改全局变量

时间:2016-04-15 07:09:03      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:pthread

#include <pthread.h>
#include <iostream>
using namespace std;

int global = 0;

void * assign_value(void *param){
    global = 3;
}

int main(){
    pthread_t thread;
    pthread_create(thread, NULL, assign_value, (void *) nullptr);
    pthread_join(thread, NULL);
    cout << global << endl;
    return 0;
}

结果输出为3,如果不加pthread_join(thread,NULL)的话,输出为0。这里,pthread_join只有当thread线程结束工作时才返回。

本文出自 “胡一刀” 博客,谢绝转载!

pthread更改全局变量

标签:pthread

原文地址:http://11190017.blog.51cto.com/11180017/1764001

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!