pthread_mutex_init()实例
2013-12-05 15:06
281 查看
百度百科链接介绍pthread_mutex_init()函数
[root@localhost cfile]# vim thread_mutex.c
+ thread_mutex.c
/*********************************************************************************
* Copyright: (C) 2013 fulinux<fulinux@sina.com>
* All rights reserved.
*
* Filename: thread_mutex.c
* Description: This file
*
* Version: 1.0.0(12/05/2013~)
* Author: fulinux <fulinux@sina.com>
* ChangeLog: 1, Release initial version on "12/05/2013 01:45:43 AM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *function(void *arg);
pthread_mutex_t mutex;
int counter = 0;
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int rc1, rc2;
char *str1 = "fulinux";
char *str2 = "wansui!";
pthread_t thread1, thread2;
pthread_mutex_init(&mutex, NULL);
if((rc1 = pthread_create(&thread1, NULL, function, str1)));
{
fprintf(stdout, "thread 1 create failed: %d\n", rc1);
}
if((rc2 = pthread_create(&thread2, NULL,function, str2)));
{
fprintf(stdout, "thread 2 create failed: %d\n", rc2);
}
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
} /* ----- End of main() ----- */
~/cfile/thread_mutex.c[+] CWD: /root/cfile Line: 42/79:8
"thread_mutex.c" 79L, 2020C written
[root@localhost cfile]# gcc thread_mutex.c -o thread -lpthread
[root@localhost cfile]# ./thread
thread 1 create failed: 0
thread 2 create failed: 0
fulinux /*每秒钟显示一个字符*/
wansui! /*每秒钟显示一个字符*/
[root@localhost cfile]#
[root@localhost cfile]# vim thread_mutex.c
+ thread_mutex.c
/*********************************************************************************
* Copyright: (C) 2013 fulinux<fulinux@sina.com>
* All rights reserved.
*
* Filename: thread_mutex.c
* Description: This file
*
* Version: 1.0.0(12/05/2013~)
* Author: fulinux <fulinux@sina.com>
* ChangeLog: 1, Release initial version on "12/05/2013 01:45:43 AM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *function(void *arg);
pthread_mutex_t mutex;
int counter = 0;
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int rc1, rc2;
char *str1 = "fulinux";
char *str2 = "wansui!";
pthread_t thread1, thread2;
pthread_mutex_init(&mutex, NULL);
if((rc1 = pthread_create(&thread1, NULL, function, str1)));
{
fprintf(stdout, "thread 1 create failed: %d\n", rc1);
}
if((rc2 = pthread_create(&thread2, NULL,function, str2)));
{
fprintf(stdout, "thread 2 create failed: %d\n", rc2);
}
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
} /* ----- End of main() ----- */
~/cfile/thread_mutex.c[+] CWD: /root/cfile Line: 42/79:8
"thread_mutex.c" 79L, 2020C written
[root@localhost cfile]# gcc thread_mutex.c -o thread -lpthread
[root@localhost cfile]# ./thread
thread 1 create failed: 0
thread 2 create failed: 0
fulinux /*每秒钟显示一个字符*/
wansui! /*每秒钟显示一个字符*/
[root@localhost cfile]#
相关文章推荐
- 互斥锁 pthread_mutex_init()函数
- 互斥锁 pthread_mutex_init()函数
- pthread_mutex_init & 互斥锁pthread_mutex_t的使用
- man手册中没有pthread_mutex_init等内容
- pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock,函数(线程互斥的例子)
- Linux学习之互斥量的封装一:封装创建(pthread_mutex_init)和销毁(pthread_mutex_destroy)
- 互斥锁 pthread_mutex_init()函数
- 线程pthread_mutex_init()锁的应用
- man pthread_mutex_init 或 man pthread_mutex_lock 没有结果的解决办
- Linux pthread_mutex_init()函数
- pthread_mutex_lock(&lock); 没有执行init 动作
- 互斥锁----pthread_mutex_t,pthread_mutex_init,pthread_mutex_lock,pthread_mutext_unlock,pthread_mutex_des
- 线程pthread_mutex_init()锁的应用
- man pthread_mutex_init 或 man pthread_mutex_lock 没有结果的解决办法
- 互斥锁----pthread_mutex_t,pthread_mutex_init,pthread_mutex_lock,pthread_mutext_unlock,pthread_mutex_des
- man pthread_mutex_init 或 man pthread_mutex_lock 没有结果的解决的方法
- pthread_mutex_init
- pthread_mutex_init() -- 互斥锁初始化|pthread_mutex_destroy()--互斥锁销毁
- ubuntu没有man pthread_mutex_init
- 实例讲解ptread_mutex_t和pthread_cond_t