多线程处理
来源:互联网 发布:淘宝潮流男装店铺 编辑:程序博客网 时间:2024/06/05 17:42
include
include
define NUM_THREADS 5
int sum = 0;
pthread_mutex_t sum_mutex;
static void *say_hello(void *args)
{
cout << “hello in thread ” << ((int )args) << endl;
pthread_mutex_lock(&sum_mutex);
cout << “before sum is ” << sum << ” in thread ” << ((int)args) << endl;
sum += ((int)args);
cout << “after sum is ” << sum << ” in thread ” << ((int)args) << endl;
pthread_mutex_unlock(&sum_mutex);
// int status = 10 + ((int )args);
pthread_exit(0);
}
int main()
{
pthread_t tids[NUM_THREADS];
cout << “hello in main .. “<< endl;
int indexes[NUM_THREADS];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_mutex_init(&sum_mutex, NULL);
for(int i = 0; i < NUM_THREADS; ++i) {
indexes[i] = i;
int ret = pthread_create(&tids[i], &attr, say_hello, (void*)&(indexes[i]));
//cout << "Current pthread id = " << tids[i] << endl;if(ret != 0) { cout << "pthread_create error: error_code = " << ret << endl;}
}
pthread_attr_destroy(&attr);
void *status;
for(int i = 0; i < NUM_THREADS; ++i) {
int ret = pthread_join(tids[i], &status);
if (ret != 0) {
cout << “pthread_join error:error_code=” << ret << endl;
} else {
//cout << “pthread_join get status:” << (long)status << endl;
}
cout << “finally sum is ” << sum << endl;
pthread_mutex_destroy(&sum_mutex);
}
// pthread_exit(NULL);
}
编译方式:
g++ -o muti_thread_test_1 muti_thread_test_1.cpp -lpthread
./muti_thread_test_1
结果:
hello in main ..
hello in thread hello in thread 01
hello in thread 2before sum is 0
in thread 0
hello in thread 3
hello in thread 4after sum is 0 in thread 0
before sum is 0 in thread 4
after sum is 4 in thread 4
before sum is 4 in thread 2
after sum is 6 in thread 2
before sum is 6 in thread 3
after sum is 9 in thread 3
before sum is 9 in thread 1
after sum is 10 in thread 1
finally sum is 10
finally sum is 10
finally sum is 10
finally sum is 10
finally sum is 10
again:
hello in main ..
hello in thread hello in thread 01
hello in thread 2
before sum is 0 in thread 2
after sum is 2 in thread 2
hello in thread 4
before sum is hello in thread 23 in thread
0
after sum is 2 in thread 0
before sum is 2 in thread 1
after sum is 3 in thread 1
before sum is 3 in thread 4
after sum is 7 in thread 4
before sum is 7 in thread 3
after sum is 10 in thread 3
finally sum is 10
finally sum is 10
finally sum is 10
finally sum is 10
finally sum is 10
- 多线程处理
- 多线程处理
- 多线程处理
- 多线程处理
- 多线程处理
- 多线程并发处理
- Delphi多线程处理(转载)
- SWT中处理多线程
- SWT中处理多线程
- C# 多线程控件处理
- C#的多线程处理
- Delphi多线程处理(转载)
- 多线程-异常处理
- java处理多线程并发
- 多线程的处理方式
- JNI 多线程的处理
- android 处理多线程 UserTask
- java 多线程异常处理
- 怎么把word转成pdf格式的文件
- 图数据库titan 和 rexster安装手册
- kairosdb中的配置说明
- ListView addHeaderView() 之后item位置不对
- 第二节课作业
- 多线程处理
- AlertDialog修改title,message,button样式
- WPF-使用代码创建Grid行与列,并将控件添加到Grid中的指定行指定列
- 软件开发中一些专业名词解释
- centos 下运行程序出现error while loading shared libraries cannot open shared object file错误
- 异或
- 机器学习相关资料搜集
- 【Android】TypedArray和obtainStyledAttributes使用
- 如何选择开源许可证?