笔记
Windows下代码创建线程+互斥锁使用
#include <iostream>
#include <pthread.h>
#include <Windows.h>
#include <string.h>
#pragma comment(lib, "pthreadVC2.lib")
#pragma warning(disable : 4996)
using namespace std;
#define NUM_THREADS 5
int g_Flag = 0;
//创建互斥锁,静态的初始化锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void printids(const char* s);
void lock(const char* s);
void unlock(const char* s);
void* thread1(void*);
void* thread2(void*);
void* thread3(void*);
int main()
{
pthread_t t1, t2, t3;
int e1 = 0, e2 = 0, e3 = 0;
void* r1=NULL, *r2=NULL, *r3=NULL;
e1 = pthread_create(&t1, NULL, thread1, NULL);
e2 = pthread_create(&t2, NULL, thread2, NULL);
e3 = pthread_create(&t3, NULL, thread3, NULL);
//调用pthread_join函数的线程将一直阻塞,直到指定的线程调用pthread_exit、
e1 = pthread_join(t1, &r1);
printf("\n没到10之前,下面的叼毛都不要动!\n");
printf("\nthread1 退出代码是:%d\n", (int)r1);
e1 = pthread_join(t2, &r2);
printf("\nthread2 退出代码是:%d\n", (int)r2);
e1 = pthread_join(t3, &r3);
printf("\nthread3 退出代码是:%d\n", (int)r3);
printids("主线程");
printf("离开主函数.");
exit(0);
}
void printids(const char* s) {
//GetCurrentProcessId获取进程id,GetCurrentThreadId获取线程id
printf("%s:进程ID为:%ld,线程ID为:%d,g_flag为:%ld \n\n", s, GetCurrentProcessId(), GetCurrentThreadId(), g_Flag);
}
void lock(const char* s) {
//pthread_mutex_lock加锁
if (pthread_mutex_lock(&mutex) != 0) {
printf("加锁出错!%s",s);
exit(0);
}
}
void unlock(const char* s) {
//pthread_mutex_unlock解锁
if (pthread_mutex_unlock(&mutex) != 0) {
printf("解锁出错!%s", s);
exit(0);
}
}
void* thread1(void* arg)
{
lock("线程1"); //加锁
printf("\n函数thread1:\n");
printids("线程1");
int i;
for (i = 0; i <= 10; i++) {
g_Flag++;
printf("【%d】", i);
}
printids("线程1");
printf("线程1离开\n");
unlock("线程1"); //解锁
pthread_exit(0);
return 0;
}
void* thread2(void* arg)
{
lock("线程2"); //加锁
printf("\n函数thread2.\n");
printids("线程2");
int i;
for (i = 0; i < 1000; i++)
g_Flag++;
printids("线程2");
printf("线程2离开.\n");
unlock("线程2"); //解锁
return (void*)2;
}
void* thread3(void* arg)
{
lock("线程3"); //加锁
printf("\n函数thread3.\n");
printids("线程3");
int i;
for (i = 0; i < 1000; i++)
g_Flag++;
printids("线程3");
printf("线程3离开.\n");
unlock("线程3"); //解锁
return (void*)3;
}
vs2019配置pthread库请参考:https://blog.csdn.net/a15322910600/article/details/119599749
想要更改线程的执行顺序,我们可以将pthread_join放在恰当的地方。另外,我们的锁主要是要保护数据操作的安全性,而不是printf函数输出的正确性。
条件变量+互斥锁
#include <iostream>
#include <pthread.h>
#include <Windows.h>
#include <synchapi.h>
#pragma comment(lib, "pthreadVC2.lib")
#pragma warning(disable : 4996)
//链表的结点
struct msg {
int num;
struct msg* next;
};
struct msg* head = NULL; //头指针
struct msg* temp = NULL; //节点指针
//静态方式初始化互斥锁和条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //互斥锁
pthread_cond_t has_producer = PTHREAD_COND_INITIALIZER; //条件变量
void* producer(void* arg) {
while (1) //线程正常不会解锁,除非收到终止信号
{
pthread_mutex_lock(&mutex); //加锁
temp = (msg*)malloc(sizeof(struct msg));
temp->num = rand() % 100 + 1;
temp->next = head;
head = temp;
printf("\n--生产者 %d \n", temp->num);
pthread_mutex_unlock(&mutex); //解锁
pthread_cond_signal(&has_producer); //唤醒消费者线程
Sleep(300); //挂起指定的秒数。为了使该线程放弃cpu,让结果看起来更加明显。
}
return NULL;
}
void* consumer(void* arg) {
while (1) //线程正常不会解锁,除非收到终止信号
{
pthread_mutex_lock(&mutex); //加锁
while (head == NULL) //如果共享区域没有数据,则解锁并等待条件变量
{
//pthread_cond_wait()被唤醒时,它解除阻塞,并且尝试获取锁
//这个pthread_cond_wait()函数可以被pthread_cond_signal()唤醒
pthread_cond_wait(&has_producer, &mutex); //通常在一个循环内使用该函数
}
temp = head;
head = temp->next;
printf("\n---------------------消费者:%d \n", temp->num);
free(temp); //删除节点,头删法
temp = NULL; //防止野指针
pthread_mutex_unlock(&mutex); //解锁
Sleep(300);
}
return NULL;
}
int main()
{
pthread_t ptid, ctid;
srand(time(NULL)); //根据时间摇一个随机数种子
pthread_create(&ptid, NULL, producer, NULL); //创建生产者线程
pthread_create(&ctid, NULL, consumer, NULL); //消费者线程
//主线程回收两个子线程
pthread_join(ptid, NULL);
pthread_join(ctid, NULL);
return 0;
}