多线程的共享变量
来源:互联网 发布:php短信验证 编辑:程序博客网 时间:2024/05/21 15:47
#include "csapp.h"#define N 2void *thread(void *vargp);char **ptr;int main(void){int i;pthread_t tid;char *msgs[N] = {"Hello from foo", "Hello from, bar" }; ptr = msgs; for (i = 0; i < N; i++) { Pthread_create(&tid, NULL, thread, &i); } Pthread_exit(NULL);}void *thread(void *vargp){int myid = *((int*)vargp);static int cnt = 0;printf("[%d]: %s (cnt = %d)\n", myid, ptr[myid], ++cnt);return NULL;}
运行:./a.out
[2]: (cnt = 1)
[2]: (cnt = 2)
因为创建了两次线程后,才会去跑线程处理函数, 当跑线程处理函数的时候,此时i = 2, 所以产生了这些线程同步的问题。
- 多线程的共享变量
- 多线程间变量的共享
- 多线程 : 多线程共享局部变量的方法
- 多线程中的使用共享变量的问题
- 多线程中的使用共享变量的问题
- 多线程中的使用共享变量的问题
- java多线程范围内的共享变量
- 多线程中的使用共享变量的问题
- Java 多线程 变量共享
- 多线程共享变量方式
- java多线程共享变量
- Thread_MultiShareData(多线程共享变量)
- 多线程共享变量
- 多线程共享变量
- 多线程:共享变量
- 多线程共享变量问题
- 多线程共享变量
- 【多线程 3】多线程间的变量共享方式
- Linux 网络编程笔记(二)
- Java各种获取系统当前时间方法 和 格式
- Android组件
- CListCtrl树的遍历 任意子节点个数
- ubuntu下打开txt文件乱码问题
- 多线程的共享变量
- db1服务器上mmm的过程
- image analogies
- Linux, gdb 调试命令
- dedehttpdown.class.php 修改备份
- Android 统计分析 SDK使用指南
- 优化ubuntu
- 超声波测距
- c++实现"四分位数"算法2