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?的更多相关文章

  1. Mutex和内存可见性

    http://ifeve.com/mutex-and-memory-visibility/ POSIX内存可见性规则 IEEE 1003.1-2008定义了XBD 4.11内存同步中的内存可见性规则. ...

  2. 【Linux】Mutex互斥量线程同步的例子

    0.互斥量  Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...

  3. pthread_create()之前的属性设置

    一.pthread_create()之前的属性设置1.线程属性设置我们用pthread_create函数创建一个线程,在这个线程中,我们使用默认参数,即将该函数的第二个参数设为NULL.的确,对大多数 ...

  4. 线程异常:undefined reference to &#39;pthread_create&#39; 处理

    源代码: #include <stdio.h> #include <pthread.h> #include <sched.h> void *producter_f ...

  5. UNIX环境高级编程——pthread_create的问题

    linux 下常用的创建多线程函数pthread_create(pthread_t * thread , pthread_attr_t * attr , void *(*start_routine)( ...

  6. mutex,thread

    //#include <stdio.h> //#include <stdlib.h> //#include <unistd.h> #include <wind ...

  7. 互斥量mutex的简单使用

    几个重要的函数: #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pt ...

  8. linux mutex

    #include <iostream> #include <queue> #include <cstdlib> #include <unistd.h> ...

  9. C++多线程同步之Mutex(互斥量)

    原文链接: http://blog.csdn.net/olansefengye1/article/details/53086141 一.互斥量Mutex同步多线程 1.Win32平台 相关函数和头文件 ...

随机推荐

  1. springMvc--接受日期类型参数处理

    这个问题,也即是springMvc如何进行参数类型的转换 , 以把client传过来一个String类型,转换为日期类型为例 步骤 1.controller /** * 接收日期类型参数 * 注意: ...

  2. Swift基础(类,结构体,函数)

    import Foundation // 创建一个类 class Student { // 属性(类的属性必须赋初值,如果不赋值,需要写自定义方法) var studentName: String v ...

  3. Django訪问量和页面PV数统计

    http://blog.csdn.net/pipisorry/article/details/47396311 以下是在模板中做一个简单的页面PV数统计.model阅读量统计.用户訪问量统计的方法 简 ...

  4. Pycharm之Terminal使用

    相当于doc命令,即工程所在目录shift+右键命令窗口打开的doc 1.清屏  ------   cls 清除屏幕上的所有显示,光标置于屏幕左上角.

  5. jsonArray和Java List对象互转,日期处理

    List转jsonArray : // 格式化日期 JsonConfig jsonConfig = new JsonConfig(); DSHJsonDateValueProcessor dshJso ...

  6. WCF学习笔记——配置服务引用

    WCF传过来的东西要序列化. 比如,在WCF服务中,象这么个方法 public IEnumerable<UserItem> GetUserList() 默认情况下,在客户端会调用,是这样: ...

  7. [C++设计模式] composite 组合模式

    组合(Composite)模式的其他翻译名称也非常多,比方合成模式.树模式等等.在<设计模式>一书中给出的定义是:将对象以树形结构组织起来,以达成"部分-总体"的层次结 ...

  8. linux中的alsa工具与Android中的tinyalsa工具【转】

    本文转载自:http://blog.csdn.net/luckywang1103/article/details/48053015 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?) ...

  9. 【POJ 1456】 Supermarket

    [题目链接] http://poj.org/problem?id=1456 [算法] 贪心 + 堆 [代码] #include <algorithm> #include <bitse ...

  10. 搭建go开发环境

    一.go下载安装 进入该网站 https://golang.google.cn/dl/  选择相应的操作系统下载安装包 Linux/Mac OS X 安装 1.下载 go1.10.3.linux-am ...