一.上集回顾
建议先学上篇博客,再向下学习,上篇博客的链接如下:
二.创建线程
1.线程的创建

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>
void* routine(void* args)
{
std::string name = static_cast<const char*>(args);
while(true)
{
std::cout << "我们是新线程,我的名字是: " << name << std::endl;
sleep(1);
}
return 0;
}
int main()
{
pthread_t tid;
pthread_create(&tid,nullptr,routine,(void*)"thread-1");
// std::cout << "new thread id: " << tid << std::endl;
printf("new thread id: 0x%lx\n",tid);
while(true)
{
std::cout << "我们是main线程..." << std::endl;
sleep(1);
}
return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>
std::string toHex(pthread_t tid)
{
char buffer[64];
snprintf(buffer,sizeof(buffer),"0x%lx",tid);
return buffer;
}
void* routine(void* args)
{
std::string name = static_cast<const char*>(args);
while(true)
{
std::cout << "我们是新线程,我的名字是: " << name << ", my tid is : " << toHex(pthread_self()) << std::endl;
sleep(1);
}
return 0;
}
int main()
{
pthread_t tid;
pthread_create(&tid,nullptr,routine,(void*)"thread-1");
// std::cout << "new thread id: " << tid << std::endl;
printf("new thread id: 0x%lx\n",tid);
while(true)
{
std::cout << "我们是main线程..." << " , my tid is : " << toHex(pthread_self()) << std::endl;
sleep(1);
}
return 0;
}

新线程和main线程谁先运行,不确定(我们要做同步或者互斥操作)
主线程和新线程基本上是瓜分时间片的(10ms -> 5个线程 -> 每个线程2ms)
2.多线程的创建
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>
std::string toHex(pthread_t tid)
{
char buffer[64];
snprintf(buffer,sizeof(buffer),"0x%lx",tid);
return buffer;
}
void* routine(void* args)
{
std::string name = static_cast<const char*>(args);
while(true)
{
std::cout << "我们是新线程,我的名字是: " << name << ", my tid is : " << toHex(pthread_self()) << std::endl;
sleep(1);
}
return 0;
}
int main()
{
pthread_t tid1;
pthread_create(&tid1,nullptr,routine,(void*)"thread-1");
pthread_t tid2;
pthread_create(&tid2,nullptr,routine,(void*)"thread-2");
pthread_t tid3;
pthread_create(&tid3,nullptr,routine,(void*)"thread-3");
pthread_t tid4;
pthread_create(&tid4,nullptr,routine,(void*)"thread-4");
printf("new thread id: 0x%lx\n",tid1);
printf("new thread id: 0x%lx\n",tid2);
printf("new thread id: 0x%lx\n",tid3);
printf("new thread id: 0x%lx\n",tid4);
while(true)
{
std::cout << "我们是main线程..." << " , my tid is : " << toHex(pthread_self()) << std::endl;
sleep(1);
}
return 0;
}

这里我们的4个线程重入了一个routine函数(这里的cout有问题)

公共资源(显示器)被多个线程访问出现了消息错乱,导致并发问题(所以routine是不可重入函数)
![]()
所有的线程都能看到我们任意的一个方法(函数),全部的线程数据共享,代码也是共享的
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>
//5.全局变量在线程内部是共享的
int gval = 100;
std::string toHex(pthread_t tid)
{
//4.进程内的函数,线程共享
char buffer[64];
snprintf(buffer,sizeof(buffer),"0x%lx",tid);
return buffer;
}
void* routine1(void* args)
{
std::string name = static_cast<const char*>(args);
while(true)
{
//3.不加保护的情况下,显示器文件就是共享资源
std::cout << "我们是新线程,我的名字是: " << name <<
", my tid is : " << toHex(pthread_self())
<< ", 全局变量(修改): " << gval<< std::endl;
gval++;
sleep(1);
}
return 0;
}
void* routine2(void* args)
{
std::string name = static_cast<const char*>(args);
while(true)
{
//3.不加保护的情况下,显示器文件

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



