windows和linux线程互斥同步机制相关函数

Windows 线程操作及同步机制相关函数

Windows 线程操作及同步机制所涉头文件包含语句、函数调用语句或函数原型如下:
(1)#include <windows.h>
(2)线程函数原型及框架

DWORD WINAPI ThreadExecutiveZGS(LPVOID lpParameter)
{
int *pID = (int*)lpParameter;
......
return 0;
}

(3)线程创建所涉相关变量及函数调用语句

HANDLE hThread[2];
int nPID0 = 0, nPID1 = 1;
if ((hThread[0] = CreateThread(NULL, 0, ThreadExecutiveZGS, &nPID0, 0, NULL)) == NULL)
{
printf("线程 ThreadExecutiveZGS-0 创建失败!\n");
exit(0);
}
if ((hThread[1] = CreateThread(NULL, 0, ThreadExecutiveZGS, &nPID1, 0, NULL)) == NULL)
{
printf("线程 ThreadExecutiveZGS-1 创建失败!\n");
exit(0);
}

(4)等待线程函数原型

DWORD WaitForMultipleObjects(
 DWORD nCount,
 CONST HANDLE *lpHandles,
 BOOL fWaitAll,
 DWORD dwMilliseconds
);

在主函数中调用本函数,以用来实现主线程对两个银行账户转账线程的等待:

WaitForMultipleObjects(2, hThread, TRUE, INFINITE);

(5)关于互斥信号量创建的函数原型及应用示例

HANDLE CreateMutex(
 LPSECURITY_ATTRIBUTES lpMutexAttributes,
 BOOL bInitialOwner,
 LPCTSTR lpName
);
HANDLE hMutex = CreateMutex(NULL, FALSE, "MutexToProtectCriticalResource");

(6)关于互斥信号量申请(上锁/等待)的函数原型及应用示例

DWORD WaitForSingleObject(
 HANDLE hHandle,
 DWORD dwMilliseconds
);
WaitForSingleObject(hMutex, INFINITE);

(7)关于互斥信号量释放(开锁/唤醒)的函数原型及应用示例

BOOL ReleaseMutex(
 HANDLE hMutex
);
ReleaseMutex(hMutex);

(8)关于线程挂起及线程句柄关闭操作的函数原型

VOID Sleep(DWORD dwMilliseconds);
BOOL CloseHandle(HANDLE hObject);

(9)关于系统时间获取的函数原型

DWORD GetTickCount(VOID); //返回值为从操作系统启动到当前时间所经过的毫秒数

Linux 线程操作及同步机制相关函数

Linux 线程操作及同步机制所涉头文件包含语句、函数调用语句或函数原型如下:
(1)#include <pthread.h>
(2)编译命令示例(末尾须加上选项-lpthread)

gcc -o ThreadCSExclusion ThreadCriticalSectionExclusion.c -lpthread

(3)线程函数原型及框架示例

void* ThreadExecutiveZGS(void* zThreadName)
{
char *pThreadName = (char*)zThreadName;
……
return (void *)0;
}

(4)线程创建所涉函数及应用示例

int pthread_creat(pthread_t *tid, const pthread_attr_t *attr, void*(*start_routine)(void *), void *arg);

返回值:成功返回 0,错误返回错误号
第一个参数 tid:线程标识符(输出型参数)
第二个参数 attr:线程属性,一般设置为 NULL(表示线程属性取缺省值)
第三个参数 start_routine:函数指针,指向新线程即将执行的代码
第四个参数 arg:新线程对应函数的参数序列封装结果

pthread_t tid1;
if (pthread_create(&tid1, NULL, ThreadExecutiveZGS, "thread1"))
{
printf("线程 ThreadExecutiveZGS-1 创建失败!\n");
exit(0);
}

(5)关于等待线程的函数原型及应用示例

int pthread_join(pthread_t tid, void **rval_ptr);

返回值:成功返回 0,错误返回错误编号
第一个参数 tid:被等待线程的标识符(输入型参数)
第二个参数 rval_ptr:指向被等待线程对应函数的返回值
在主函数中调用本函数,以用来实现主线程对指定银行账户转账线程的等待:

void *ret1;
pthread_join(tid1, &ret1);

(6)关于互斥锁初始化、上锁/开锁的函数原型及应用示例

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/*上锁*/
if (pthread_mutex_lock(&mutex) != 0)
{
printf("上锁失败!\n");
exit(1);
}
/*开锁*/
if (pthread_mutex_unlock(&mutex) != 0)
{
printf("开锁失败!\n ");
exit(1);
}

(7)关于系统时间获取的函数原型

#include <time.h>
time_t time(time_t *t);

获取和返回从公元 1970 年 1 月 1 日的 0 时 0 分 0 秒【UTC 时间】算起到现在所经过的
秒数。

#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);

获取系统当前精确时间(从公元 1970 年 1 月 1 日 0 时 0 分 0 秒 0 微秒【UTC 时间】算
起到现在所经过的秒数和微秒数)存放于参数变量 tv 中(调用时相应时区信息参数变量一般
置为 NULL)。其中时间信息结构体定义如下:

struct timeval
{
long int tv_sec; /*秒数*/
long int tv_usec; /*微秒数*/
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值