DEV Community

海前 王
海前 王

Posted on

thread3

/*#include <stdio.h>
#include <windows.h>
#include <process.h>

#define NUM_THREAD  50
unsigned WINAPI threadInc(void* arg);
unsigned WINAPI threadDes(void* arg);

long long num = 0;
HANDLE hMutex;

int main(int argc, char* argv[])
{
    HANDLE tHandles[NUM_THREAD];
    int i;

    hMutex = CreateMutex(NULL, FALSE, NULL);
    for (i = 0; i < NUM_THREAD; i++)
    {
        if (i % 2)
            tHandles[i] = (HANDLE)_beginthreadex(NULL, 0, threadInc, NULL, 0, NULL);
        else
            tHandles[i] = (HANDLE)_beginthreadex(NULL, 0, threadDes, NULL, 0, NULL);
    }

    WaitForMultipleObjects(NUM_THREAD, tHandles, TRUE, INFINITE);
    CloseHandle(hMutex);
    printf("result: %lld \n", num);
    return 0;
}

unsigned WINAPI threadInc(void* arg)
{
    int i;

    WaitForSingleObject(hMutex, INFINITE);
    for (i = 0; i < 500000; i++)
        num += 1;
    ReleaseMutex(hMutex);

    return 0;
}
unsigned WINAPI threadDes(void* arg)
{
    int i;

    WaitForSingleObject(hMutex, INFINITE);
    for (i = 0; i < 500000; i++)
        num -= 1;
    ReleaseMutex(hMutex);

    return 0;
}
*/
/*
#include <stdio.h>
#include <windows.h>
#include <process.h>

#define NUM_THREAD  50
unsigned WINAPI threadInc(void* arg);
unsigned WINAPI threadDes(void* arg);
long long num = 0;

int main(int argc, char* argv[])
{
    HANDLE tHandles[NUM_THREAD];
    int i;

    printf("sizeof long long: %d \n", sizeof(long long));
    for (i = 0; i < NUM_THREAD; i++)
    {
        if (i % 2)
            tHandles[i] = (HANDLE)_beginthreadex(NULL, 0, threadInc, NULL, 0, NULL);
        else
            tHandles[i] = (HANDLE)_beginthreadex(NULL, 0, threadDes, NULL, 0, NULL);
    }

    WaitForMultipleObjects(NUM_THREAD, tHandles, TRUE, INFINITE);
    printf("result: %lld \n", num);
    return 0;
}

unsigned WINAPI threadInc(void* arg)
{
    int i;
    for (i = 0; i < 500000; i++)
        num += 1;
    return 0;
}
unsigned WINAPI threadDes(void* arg)
{
    int i;
    for (i = 0; i < 500000; i++)
        num -= 1;
    return 0;
}
*/
/*
#include <stdio.h>
#include <windows.h>
#include <process.h>


unsigned int __stdcall ThreadFun(LPVOID p)
{
    int cnt = *((int*)p);
    for (int i = 0; i < cnt; i++)
    {
        Sleep(1000);
        puts("running thread");
    }
    return 0;
}


int main()
{
    printf("main begin\n");
    int iParam = 5;
    unsigned int dwThreadID;
    DWORD wr;

    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadFun,
        (void*)&iParam, 0, &dwThreadID);

    if (hThread == NULL)
    {
        puts("_beginthreadex() error");
        return -1;
    }
    // 
    printf("WaitForSingleObject begin\n");
    if ((wr = WaitForSingleObject(hThread, INFINITE)) == WAIT_FAILED)
    {
        puts("thread wait error");
        return -1;
    }
    printf("WaitForSingleObject end\n");

    printf("main end\n");
    system("pause");
    return 0;
}
*/
/*
#include <stdio.h>
#include <windows.h>
#include <process.h>   

unsigned WINAPI ThreadFunc(void* arg);

int main(int argc, char* argv[])
{
    HANDLE hThread;
    unsigned threadID;
    int param = 5;

    hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, (void*)&param, 0, &threadID);
    if (hThread == 0)
    {
        puts("_beginthreadex() error");
        return -1;
    }
    Sleep(3000);
    puts("end of main");
    system("pause");
    return 0;
}


unsigned WINAPI ThreadFunc(void* arg)
{
    int i;
    int cnt = *((int*)arg);
    for (i = 0; i < cnt; i++)
    {
        Sleep(1000);  puts("running thread");
    }
    return 0;
}
*/

//#include <stdio.h>
//#include <windows.h>
//#include <process.h>
//#define 小米 0
//DWORD WINAPI ThreadFun(LPVOID p)
//{
//    int iMym = *((int*)p);
//    printf("我是子线程,PID = %d,iMym = %d\n", GetCurrentThreadId(), iMym);
//    return 0;
//}
//
//int main()
//{
//    printf("main begin\n");
//
//    HANDLE hThread;
//    DWORD dwThreadID;
//    int m = 100;
//    hThread = CreateThread(NULL, 0, ThreadFun, &m, 0, &dwThreadID);
//    printf("我是主线程,PID = %d\n", GetCurrentThreadId());
//    CloseHandle(hThread);
//    Sleep(2000);
//    system("pause");
//  
//    return 0;
//}

#include <iostream>

int main()
{
    auto a = 10zu;
    auto b = 1u;
    std::cout << std::boolalpha;
    std::cout << std::is_same<decltype(a), decltype(b)>::value << std::endl;  // 输出false
    std::cout << std::is_same<decltype(a), long long>::value << std::endl;  // 输出false
    std::cout << std::is_same<decltype(a), unsigned long long>::value << std::endl; // 输出false
    std::cout << std::is_same<decltype(a), std::size_t>::value << std::endl;  // 输出true
    return 0;
}

/*#include <iostream>
#include <windows.h>
using namespace std;

DWORD WINAPI ThreadFunc(LPVOID pParam)
{
    cout << "子线程" << endl;
    return 0;
}

int main()
{
    HANDLE hThread = CreateThread(NULL, 0, ThreadFunc, 0, 0, 0);
    Sleep(2000);
    CloseHandle(hThread);
}

#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>

void f1(int n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread 1 executing\n";
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
    std::cout << "\t";
}

void f2(int& n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread 2 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}

int main()
{
    int n = 0;
    std::thread t1;                   // t1 is not a thread
    std::thread t2(f1, n + 1);        // 值传递
    std::thread t3(f2, std::ref(n));  // 引用传递
    std::thread t4(std::move(t3));    // 移动构造函数
    t2.join();
    t4.join();
    std::cout << "Final value of n is " << n << '\n';
}*/
/**
#include <iostream>
#include <chrono>
#include <thread>

void independentThread()
{
    std::cout << "Starting concurrent thread.\n";
    //std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "Exiting concurrent thread.\n";
}

void threadCaller()
{
    std::cout << "Starting thread caller.\n";
    std::thread t(independentThread);
    t.detach();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Exiting thread caller.\n";
}

int main()
{
    threadCaller();
    //std::this_thread::sleep_for(std::chrono::seconds(5));
}*/

#import <iostream>;
#include <windows.h>

using namespace std;
//互斥对象
HANDLE hMutex;
DWORD WINAPI Fun1Proc(LPVOID);
DWORD WINAPI Fun2Proc(LPVOID);
int tickets = 20;
void main() {
    HANDLE hThread1;
    HANDLE hThread2;
    //创建线程
    hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);
    hThread2 = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL);
    CloseHandle(hThread1);
    CloseHandle(hThread2);

    //创建互斥对象
    //TRUE: 创建的线程拥有该互斥对象的使用权
    hMutex = CreateMutex(NULL, TRUE, L"tickets");
    if (hMutex)
    {
        if (ERROR_ALREADY_EXISTS == GetLastError())
        {
            cout << "只有一个实例能够运行" << endl;
            return;
        }
    }
    //等待互斥对象有信号
    WaitForSingleObject(hMutex, INFINITE);
    ReleaseMutex(hMutex);
    ReleaseMutex(hMutex);
    //主线程休息4s,此时运行别的线程
    Sleep(4000);

}
//线程1的入口函数
DWORD WINAPI Fun1Proc(LPVOID lp) {

    WaitForSingleObject(hMutex, INFINITE);
    cout << "thread 1 is running......" << endl;
    return 0;
}
//线程2的入口函数
DWORD WINAPI Fun2Proc(LPVOID lp) {

    WaitForSingleObject(hMutex, INFINITE);
    cout << "thread 2 is running......" << endl;
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)