How to use pthread_create && mutex?
1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void *thread_function(void *arg)
{
int i;
for (i=0; i<5; i++){
printf("Thread says hi!\n");
sleep(1);
}
return NULL;
}
int main(void)
{
pthread_t mythread;
if ( pthread_create( &mythread, NULL, thread_function, NULL) ){
printf("error creating thread.\n");
abort();
}
if ( pthread_join( mythread, NULL )){
printf("error joining thread.");
abort();
}
exit(0);
}
gcc -o pthread1 pthread1.c -lpthread
2
#cat thread2.c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int myglobal;
void *thread_function(void *arg)
{
int i,j;
for (i=0; i<20; i++){
i=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
}
return NULL;
}
int main(void)
{
pthread_t mythread;
int i;
if (pthread_create(&mythread, NULL, thread_function, NULL)){
printf("Error creating thread.");
abort();
}
for (i=0; i<20; i++){
myglobal=myglobal+1;
printf("o");
fflush(stdout);
sleep(1);
}
if (pthread_join(mythread, NULL)){
printf("Error joining.");
abort();
}
printf("\nmyglobal equals %d\n ",myglobal);
exit(0);
}
3. Use mutex
#cat thread3.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
int myglobal;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
void *thread_function(void *arg)
{
int i,j;
for (i=0; i<10; i++){
pthread_mutex_lock(&mymutex);
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
pthread_mutex_unlock(&mymutex);
}
return NULL;
}
int main(void)
{
pthread_t mythread;
int i;
if (pthread_create(&mythread, NULL, thread_function, NULL)){
printf("Error creating function;");
abort();
}
for (i=0; i<10; i++){
pthread_mutex_lock(&mymutex);
myglobal=myglobal+1;
pthread_mutex_unlock(&mymutex);
printf("o");
fflush(stdout);
sleep(1);
}
if (pthread_join(mythread, NULL)){
printf("Error joining thread.");
abort();
}
printf("myglobal equals %d\n",myglobal);
exit(0);
}
REF
https://www.ibm.com/developerworks/cn/linux/thread/posix_thread2/index.html
How to use pthread_create && mutex?的更多相关文章
- Mutex和内存可见性
http://ifeve.com/mutex-and-memory-visibility/ POSIX内存可见性规则 IEEE 1003.1-2008定义了XBD 4.11内存同步中的内存可见性规则. ...
- 【Linux】Mutex互斥量线程同步的例子
0.互斥量 Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...
- pthread_create()之前的属性设置
一.pthread_create()之前的属性设置1.线程属性设置我们用pthread_create函数创建一个线程,在这个线程中,我们使用默认参数,即将该函数的第二个参数设为NULL.的确,对大多数 ...
- 线程异常:undefined reference to 'pthread_create' 处理
源代码: #include <stdio.h> #include <pthread.h> #include <sched.h> void *producter_f ...
- UNIX环境高级编程——pthread_create的问题
linux 下常用的创建多线程函数pthread_create(pthread_t * thread , pthread_attr_t * attr , void *(*start_routine)( ...
- mutex,thread
//#include <stdio.h> //#include <stdlib.h> //#include <unistd.h> #include <wind ...
- 互斥量mutex的简单使用
几个重要的函数: #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pt ...
- linux mutex
#include <iostream> #include <queue> #include <cstdlib> #include <unistd.h> ...
- C++多线程同步之Mutex(互斥量)
原文链接: http://blog.csdn.net/olansefengye1/article/details/53086141 一.互斥量Mutex同步多线程 1.Win32平台 相关函数和头文件 ...
随机推荐
- SharePoint 2013:解决添加域名后每次都需要登录的问题
在SharePoint 2013中,当我们添加一个域名给SP后(添加域名的方法请参考此文:http://www.cnblogs.com/jianyus/archive/2013/08/10/32494 ...
- HDU4882ZCC Loves Codefires(贪心)
ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- CSS3选择器(全)
CSS选择器复习 通用选择器:* 选择到全部的元素 选择子元素:> 选择到元素的直接后代(第一级子元素) 相邻兄弟选择器:+ 选择到紧随目标元素后的第一个元素 普通兄弟选择器:~ 选择到紧随其后 ...
- xpath元素查找提示is not clickable
1.用xpath可以在chrome找到 $x("//mandatory-config-dialog[@is-show='isShowMandatoryConfig']/div/div[2]/ ...
- jeecg-easypoi-2.0.3版本号公布
EasyPOI是在jeecg的poi模块基础上,继续开发独立出来的,能够说是2.0版本号,EasyPoi封装的目的和jeecg一致,争取让大家write less do more ,在这个思路上eas ...
- B3109 [cqoi2013]新数独 搜索dfs
就是基于普通数独上的一点变形,然后就没什么了,普通数独就是进行一边dfs就行了. 题干: 题目描述 输入格式 输入一共15行,包含一个新数独的实例.第奇数行包含左右方向的符号(<和>),第 ...
- 把一个文件夹下的多个csv文件合并到一个excel的多个sheet
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import pandas as pd import os import re if __name__ = ...
- WingIDE4.1 破解及支持中文设置
1.下面提供最新版本的破解方法. 先到http://wingware.com/downloads/wingide下载最新版本的IDE. 安装之前,先修改时间到一个月前. 安装 安装之后然后获取试用版的 ...
- Constructing Roads(spfa)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2493 #include <stdio.h ...
- [Luogu 2331] [SCOI2005]最大子矩阵
[Luogu 2331] [SCOI2005]最大子矩阵 题目描述 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. 输入输出格式 ...