zoukankan      html  css  js  c++  java
  • 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语言中重要函数
    python 类属性、对象属性
    windows下PIP安装模块编码错误解决
    python爬取百思不得姐视频
    ubuntu下刷新dns
    pycharm设置安装python第三方插件
    python将str转换成字典
    pyqt加载图片
    Python端口扫描器
    自己构造用于异步请求的JSON数据
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/1602378.html
Copyright © 2011-2022 走看看