C++11并发之std::thread

https://www.cnblogs.com/mmc9527/p/10427924.html

#include<thread>  

        std::thread task_;  //类成员变量,不能是局部变量
			task_ =std::thread(&RtmpStream::ReadRtpPacketThread, this); 


void RtmpStream::ReadRtpPacketThread()
{
    while(m_bReadRtpThreadRun )
    {
    }
}	



	void RtmpStream::stop()
	{
		m_bReadRtpThreadRun = false;
		if (task_.joinable())
		   task_.join();
	}

https://www.cnblogs.com/c4isr/p/9205164.html

#include <thread>
#include <iostream>

class Wrapper {
  public:
      void member1() {
          std::cout << "i am member1" << std::endl;
      }
      void member2(const char *arg1, unsigned arg2) {
          std::cout << "i am member2 and my first arg is (" << arg1 << ") and second arg is (" << arg2 << ")" << std::endl;
      }
      std::thread member1Thread() {
          return std::thread(&Wrapper::member1, this);
      }
      std::thread member2Thread(const char *arg1, unsigned arg2) {
          return std::thread(&Wrapper::member2, this, arg1, arg2);
      }
};

int main() {
  Wrapper *w = new Wrapper();
  std::thread tw1 = w->member1Thread();
  tw1.join();
  w->member2Thread("hello", 100).detach();
return 0; }

_beginThreadex

https://blog.csdn.net/p312011150/article/details/81538247

华为OJ多线程问题

https://blog.csdn.net/zz460833359/article/details/48215533

#include <iostream>   
#include <Windows.h>   
#include <process.h>   
using namespace std;  
  
char g_write[1032];  
int num=0;  
int iLoop=0;  
HANDLE GetThreadEve[4];  
HANDLE hThread[4];  
  
unsigned int __stdcall ThreadFun1(PVOID pM)  
{       
    int M=*(int *)pM;           
    iLoop=M;     
    while(M!=0)  
    {  
        WaitForSingleObject(GetThreadEve[0],INFINITE);  
        g_write[num]='A';  
        num++;  
        SetEvent(GetThreadEve[1]);  
        M--;  
    }  
    _endthreadex(0);  
    return 0;  
}  
unsigned int __stdcall ThreadFun2(PVOID pM)  
{  
    for (int i=0;i<iLoop;i++)  
    {  
        WaitForSingleObject(GetThreadEve[1],INFINITE);  
        g_write[num]='B';  
        num++;  
        SetEvent(GetThreadEve[2]);  
    }  
    _endthreadex(0);  
    return 0;  
}  
unsigned int __stdcall ThreadFun3(PVOID pM)  
{  
    for (int i=0;i<iLoop;i++)  
    {  
        WaitForSingleObject(GetThreadEve[2],INFINITE);  
        g_write[num]='C';  
        num++;  
        SetEvent(GetThreadEve[3]);  
    }  
    _endthreadex(0);  
    return 0;  
}  
unsigned int __stdcall ThreadFun4(PVOID pM)  
{  
    for (int i=0;i<iLoop;i++)  
    {  
        WaitForSingleObject(GetThreadEve[3],INFINITE);  
        g_write[num]='D';  
        num++;  
        SetEvent(GetThreadEve[0]);  
    }  
    _endthreadex(0);  
    return 0;  
}  
void init()  
{  
    GetThreadEve[0] = CreateEvent(NULL,FALSE,TRUE,NULL);  
    GetThreadEve[1] = CreateEvent(NULL,FALSE,FALSE,NULL);  
    GetThreadEve[2] = CreateEvent(NULL,FALSE,FALSE,NULL);  
    GetThreadEve[3] = CreateEvent(NULL,FALSE,FALSE,NULL);  
    memset(g_write,NULL,sizeof(g_write));  
}  
void Release()  
{  
    CloseHandle(hThread[0]);  
    CloseHandle(hThread[1]);  
    CloseHandle(hThread[2]);  
    CloseHandle(hThread[3]);  
}  
int main()  
{  
    int Num;  
    cin>>Num;  
    int *N=NULL;  
    init();  
    N=(int*)malloc(sizeof(int));  
    *N=Num;  
    hThread[0]=(HANDLE)_beginthreadex(NULL,0,ThreadFun1,N,0,NULL);  
    hThread[1]=(HANDLE)_beginthreadex(NULL,0,ThreadFun2,NULL,0,NULL);  
    hThread[2]=(HANDLE)_beginthreadex(NULL,0,ThreadFun3,NULL,0,NULL);  
    hThread[3]=(HANDLE)_beginthreadex(NULL,0,ThreadFun4,NULL,0,NULL);  
    WaitForMultipleObjects(4,hThread,TRUE,INFINITE);  
    Release();  
    cout<<g_write<<endl;  
    return 0;  
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值