本篇文章给大家谈谈WindowsAPICreateMutex和C++11thread类多线程编程比较,以及thread的api的知识点,同时本文还将给你拓展.net–AppDomain.GetCurr
本篇文章给大家谈谈Windows API CreateMutex和C++11 thread类多线程编程比较,以及thread的api的知识点,同时本文还将给你拓展.net – AppDomain.GetCurrentThreadID对于Windows API调用的Thread.ManagedThreadID?、.net – Windows.System.Threading.ThreadPool和System.Threading.ThreadPool有什么区别?、c – 使用boost :: mutex和MFC线程(AfxBeginThread)?、c – 面试:由_beginthread(ex)创建的pthread和windows线程有什么区别?等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- Windows API CreateMutex和C++11 thread类多线程编程比较(thread的api)
- .net – AppDomain.GetCurrentThreadID对于Windows API调用的Thread.ManagedThreadID?
- .net – Windows.System.Threading.ThreadPool和System.Threading.ThreadPool有什么区别?
- c – 使用boost :: mutex和MFC线程(AfxBeginThread)?
- c – 面试:由_beginthread(ex)创建的pthread和windows线程有什么区别?
Windows API CreateMutex和C++11 thread类多线程编程比较(thread的api)
1、 C++11 thread类多线程编程
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <Windows.h>
#include <mutex>
using namespace std;
mutex mu; //线程互斥对象
int totalNum = 100;
void thread01()
{
while (totalNum > 0)
{
mu.lock(); //同步数据锁
cout << totalNum << endl;
totalNum--;
Sleep(100);
mu.unlock(); //解除锁定
}
}
void thread02()
{
while (totalNum > 0)
{
mu.lock();
cout << totalNum << endl;
totalNum--;
Sleep(100);
mu.unlock();
}
}
//两个线程买票,到哪个线程,锁住,totalNum票总数减1,解锁
int _tmain(int argc, _TCHAR* argv[])
{
thread task01(thread01);
thread task02(thread02);
task01.detach();
task02.detach();
system("pause");
return 0;
}
2、C++使用Windows API CreateMutex函数多线程编程
#include <iostream>
#include <Windows.h>
using namespace std;
HANDLE hMutex = NULL; //互斥量
int totalNum = 100;
DWORD WINAPI thread01(LPVOID lvParamter)
{
// for (int i = 0; i < 10; i++)
// {
while (TRUE)
{
WaitForSingleObject(hMutex, INFINITE); //互斥锁
if (totalNum > 0)
{
totalNum--;
cout << "Thread 01 is working! == "<<totalNum << endl;
}
ReleaseMutex(hMutex); //释放互斥锁
Sleep(10);
}
// }
return 0;
}
DWORD WINAPI thread02(LPVOID lvParamter)
{
// for (int i = 0; i < 10; i++)
// {
while (TRUE)
{
WaitForSingleObject(hMutex, INFINITE); //互斥锁
if (totalNum > 0)
{
totalNum--;
cout << "Thread 02 is working! == "<<totalNum << endl;
}
ReleaseMutex(hMutex); //释放互斥锁
Sleep(10);
}
// }
return 0;
}
//两个线程买票,到哪个线程,锁住,totalNum票总数减1,解锁
int _tmain(int argc, _TCHAR* argv[])
{
hMutex = CreateMutex(NULL, FALSE, (LPCWSTR)"Test"); //创建互斥量
HANDLE hThread = CreateThread(NULL, 0, thread01, NULL, 0, NULL); //创建线程01
hThread = CreateThread(NULL, 0, thread02, NULL, 0, NULL); //创建线程01
CloseHandle(hThread); //关闭句柄
system("pause");
return 0;
}
.net – AppDomain.GetCurrentThreadID对于Windows API调用的Thread.ManagedThreadID?
我发现示例代码声明了以下函数:
<DllImport("User32.dll",CharSet:=CharSet.Auto,_ CallingConvention:=CallingConvention.StdCall)> _ Public Overloads Shared Function SetwindowsHookEx _ (ByVal idHook As Integer,ByVal HookProc As CallBack,_ ByVal hInstance As IntPtr,ByVal wParam As Integer) As Integer End Function
调用函数时,使用以下代码:
hHook = SetwindowsHookEx(WH_MOUSE,_ hookproc,_ IntPtr.Zero,_ AppDomain.GetCurrentThreadId())
但是Appdomain.GetCurrentThreadID会生成警告:“公共共享函数GetCurrentThreadId()As Integer”已过时:AppDomain.GetCurrentThreadId已被弃用,因为当托管线程在光纤上运行时(即轻量级线程)不提供稳定的Id.要获得受管线程的稳定标识符,请使用Thread上的ManagedThreadId属性.
我试过使用ManagedThreadID,但这不行.返回的线程ID似乎是线程的逻辑线程ID,因为它在.net运行时运行,而不是Win32线程标识符.
调用函数ith AppDomain.GetCurrentThreadID有效,但我真的希望有一个“稳定的标识符”为我的线程.
有人向我解释,是否可以在这个上下文中使用ManagedThreadID(我假设没有),如果没有,我需要避免的事情,以阻止AppDomain.CurrentThreadID变得“不稳定”?
干杯
ManagedThreadId存在的原因是因为本机和被管线程之间不一定是1-1映射.只要本地线程与其替换的本机线程兼容,CLR可以自由使用多个本机线程来运行单个托管线程.它不能例如在一个不同的COM公寓.
在某些方面你有点困在这里. AFAIK,没有办法100%保证你将有一个给定的线程相同的本机线程.如果您运行WinForms或WPF应用程序,并且在UI线程上发生对本地代码的调用,则可以实现非常高级别的保证.原因是这两个UI框架都存在于STA公寓中,这使得它非常困难(如果可能的话)CLR从您下面切换出来.
简短版本:如果您使用的是WinForms或WPF应用程序,并且在UI线程上运行该应用程序,则可以为此标识符提供合理的稳定级别.
.net – Windows.System.Threading.ThreadPool和System.Threading.ThreadPool有什么区别?
解决方法
如果您正在为WP8或Windows应用商店开发XAML应用程序,请使用Windows.System版本.
c – 使用boost :: mutex和MFC线程(AfxBeginThread)?
解决方法
Boost没有对直接创建线程(Windows API)或通过Boost创建事实做出特殊假设.
简短回答:
没问题.
c – 面试:由_beginthread(ex)创建的pthread和windows线程有什么区别?
解决方法
If I wanted to create a portable cross-platform
C++ binary,I’d use pthreads and use
the pthread implementation for
windows. If I wanted to create a
windows-specific C++ binary,I’d use
beginthread and avoid the 3rd party
dependency on the pthread library.
如果他们真的想知道描述两者之间差异的错综复杂的内部细节,你应该考虑两次在那里工作.除非是用于逆向工程.
关于Windows API CreateMutex和C++11 thread类多线程编程比较和thread的api的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.net – AppDomain.GetCurrentThreadID对于Windows API调用的Thread.ManagedThreadID?、.net – Windows.System.Threading.ThreadPool和System.Threading.ThreadPool有什么区别?、c – 使用boost :: mutex和MFC线程(AfxBeginThread)?、c – 面试:由_beginthread(ex)创建的pthread和windows线程有什么区别?等相关知识的信息别忘了在本站进行查找喔。
本文标签: