• thread demo


    // testCPP.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <stdio.h>
    #include <tchar.h>
    #include <time.h>
    #include <string>
    #include <iostream>
    //#include  <fstream>
    #include <windows.h>
    #define DESC "test thread"   //aaaa
    using namespace std;


    //void  openFile(std::ostream *vsFile)
    //{
    // if(*vsFile)
    // {
    // *vsFile<<"this is just for test \r\n"<<std::endl;
    // }
    // else
    // {
    // cout<<"open file error!"<<endl;
    // }
    //}

    HANDLE m_mutex;
    DWORD WINAPI ThreadProc1(LPVOID lpParameter);
    DWORD WINAPI ThreadProc2(LPVOID lpParameter);
    int _tmain(int argc, _TCHAR* argv[])
    {
    int is=2;
    long j=3;
    is=(int)j;
    HANDLE thread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
    HANDLE thread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
    // CloseHandle(thread1);
    // CloseHandle(thread2);
    string straa="";
    straa=DESC;
    int i=strlen(DESC);
    cout<<i<<"---lenght\r\n"<<endl;
    cout<<straa<<endl;
    m_mutex=CreateMutex(NULL,FALSE,NULL);
    getchar();
    return 0;
    }

    DWORD WINAPI ThreadProc1(LPVOID lpParameter)
    {
    while(1)
    {
      WaitForSingleObject(m_mutex,INFINITE);
      cout<<"thread 1 run\r\n"<<endl;
      ReleaseMutex(m_mutex);
      Sleep(800);
    //   break;
    }
     return 0;
    }
    DWORD WINAPI ThreadProc2(LPVOID lpParameter)
    {
    while(1)
    {
      WaitForSingleObject(m_mutex,INFINITE);
      cout<<"thread 2 run\r\n"<<endl;
      ReleaseMutex(m_mutex);
      Sleep(800);
    //   break;
    }
     return 0;
    }


    //根据分区,有十个线程来处理这个文件的生成

  • 相关阅读:
    短信猫软件的实现(C#)<八>7bitPDU的解码
    短信猫软件的实现(C#)<七>短信猫(简化测试版)实现
    短信猫软件的实现(C#)<十一>软件实现(完结篇)
    我的EDA课程设计 Verilog HDL 自动售票机的实现
    Linux JNI(1)
    Notes
    Linux 线程属性
    Linux shared lib
    Linux 几个调试命令
    Java工具
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/1602378.html
Copyright © 2020-2023  润新知