Linux 线程与互斥锁的使用
2012-11-26 20:41
204 查看
互斥锁的基本函数和用于信号量的函数非常相似:
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t, *mutexattr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destory(pthread_mutex_t *mutex);
以下是代码实例:
#include <iostream>
#include <string>
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
using namespace std;
#define SIZE 1024
char buffer[SIZE];
void *thread_function(void *arg);
pthread_mutex_t mutex;
int main()
{
int res;
pthread_t ThreadID;
void* ThreadResult;
res = pthread_mutex_init(&mutex, NULL);
if (res != 0)
{
perror("MUTEX INIT FAILED!");
exit(EXIT_FAILURE);
}
res = pthread_create(&ThreadID, NULL, thread_function, NULL);
if (res != 0)
{
perror("Thread Create Failed!");
exit(EXIT_FAILURE);
}
printf("ThreadID:%d\n", ThreadID);
while(1)
{
pthread_mutex_lock(&mutex);
scanf("%s", buffer);
pthread_mutex_unlock(&mutex);
if (strncmp("end", buffer, 3) == 0)
{
break;
}
sleep(1);
}
res = pthread_join(ThreadID, &ThreadResult);
if (res != 0)
{
perror("Thread Join Failed");
exit(EXIT_FAILURE);
}
printf("Thread join\n");
pthread_mutex_destroy(&mutex);
return(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
sleep(1);
while(1)
{
pthread_mutex_lock(&mutex);
printf("you input %d \n", strlen(buffer));
pthread_mutex_unlock(&mutex);
if (strncmp("end", buffer, 3) == 0)
{
break;
}
sleep(1);
}
pthread_exit(NULL);
}
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t, *mutexattr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destory(pthread_mutex_t *mutex);
以下是代码实例:
#include <iostream>
#include <string>
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
using namespace std;
#define SIZE 1024
char buffer[SIZE];
void *thread_function(void *arg);
pthread_mutex_t mutex;
int main()
{
int res;
pthread_t ThreadID;
void* ThreadResult;
res = pthread_mutex_init(&mutex, NULL);
if (res != 0)
{
perror("MUTEX INIT FAILED!");
exit(EXIT_FAILURE);
}
res = pthread_create(&ThreadID, NULL, thread_function, NULL);
if (res != 0)
{
perror("Thread Create Failed!");
exit(EXIT_FAILURE);
}
printf("ThreadID:%d\n", ThreadID);
while(1)
{
pthread_mutex_lock(&mutex);
scanf("%s", buffer);
pthread_mutex_unlock(&mutex);
if (strncmp("end", buffer, 3) == 0)
{
break;
}
sleep(1);
}
res = pthread_join(ThreadID, &ThreadResult);
if (res != 0)
{
perror("Thread Join Failed");
exit(EXIT_FAILURE);
}
printf("Thread join\n");
pthread_mutex_destroy(&mutex);
return(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
sleep(1);
while(1)
{
pthread_mutex_lock(&mutex);
printf("you input %d \n", strlen(buffer));
pthread_mutex_unlock(&mutex);
if (strncmp("end", buffer, 3) == 0)
{
break;
}
sleep(1);
}
pthread_exit(NULL);
}
相关文章推荐
- [Linux线程]线程的同步--使用互斥锁完成线程同步
- linux线程锁使用实例
- Linux中线程使用详解
- linux中使用pthread_kill函数测试线程是否存活 .
- linux线程使用读写锁共享全局变量
- Linux线程-互斥锁pthread_mutex_t
- 在Linux中使用线程
- Linux中使用Shell脚本查看Java线程的CPU使用情况
- Linux下面的线程锁,条件变量以及信号量的使用
- Linux线程-互斥锁pthread_mutex_t http://blog.csdn.net/zmxiangde_88/article/details/7998458
- Linux下Java线程详细监控和其dump的分析使用—-分析Java性能瓶颈
- 转帖多线程编程使用互斥锁同步线程
- Linux下面的线程锁,条件变量以及信号量的使用
- 在Linux中使用线程
- LINUX中使用PTHREAD_KILL函数测试线程是否存活的例子
- linux下互斥锁的使用
- <linux线程>同步互斥机制之【互斥锁】
- Linux多线程——使用信号量同步线程
- 最基本的linux线程编写和锁的使用
- Linux线程-互斥锁pthread_mutex_t应用