I'm porting one Linux application to Windows using Visual Studio 2015. I know there is pthread for Windows project out there but I want to convert all pthread calls to c11 standard thread libraries. However, I found the document extremely difficult to find. To be specific, I'm trying to find the equivalent of pthread_attr_t in C11 thread but could not find any.
Can somebody point out reference materials? Thanks!
There is no equivalent to attributes for C11 thread creation. The only additional state that a thread can have is to be detached. This you achieve by calling thrd_detach.
Related
I looked for the implementation of this function in MSVC STL source on github, but found no code other than setting the synchronization flag.
Next, I wrote a simple program with a call to this function, and ran it under a debugger, hoping to find a reading of the address of this flag. But during the operation of the program, nothing reads this flag, except for the function itself.
Why didn't Microsoft implement this feature?
After that, I went to cppreference and was surprised by the detailed description of the function. Where does this information come from? It turns out that GCC is used in the cppreference examples, I went to look at libstdc++ source, where I found that this function is fully implemented.
Why did Microsoft decide to do this? Maybe their goal is to ensure the security and full synchronization of I/O streams?
"setting the synchronization flag" and returning the old one is all it is supposed to do as far as the standard goes: https://eel.is/c++draft/ios.base#ios.members.static-1
Otherwise, called with a false argument, it allows the standard streams to operate independently of the standard C streams.
"allows" doesn't mean "shall". It just means if you didn't call it with false, library is not allowed to buffer.
Cppreference says the same https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio
If the synchronization is turned off, the C++ standard streams are allowed to buffer their I/O independently
Microsoft here and
LLVM here did not make use of the allowance and simply update the static boolean flag value that is not otherwise used. GNU libstdc++ is one current implementation that took a step further and gave unsynchronized C++ streams their own buffers here
I have a C++ application which consists of unmanaged C++, managed C++ and c#. In the unmanaged part I'm trying to create a thread safe collection using std::mutex.
However when I use the mutex I get the following error;
error C1189: #error : <mutex> is not supported when compiling with /clr or /clr:pure.
Any idea why I can't use the mutex?
Can someone recommend a replacement for it so that I can create a thread-safe unmanaged collection?
It is not supported because the std::mutex implementation uses GetCurrentThreadId(). That's a winapi function that is not supposed to be use in managed code since it might be running on a custom CLR host that doesn't use threads to implement threading.
This is the good kind of problem to have, it shows that you are building your code wrong. Your native C++ is being compiled with /clr in effect. Which works rather too well, all C++03 compliant code can be compiled to MSIL and get just-in-time compiled at runtime, just like managed code. You don't want this to happen, your native C++ code should be compiled to machine code and get the compile-time code optimizer love.
Turn off the /clr option for this source code file, and possibly others, in your project. Right-click + Properties, General. If the mutex appears in the .h file that you have to #include in a C++/CLI source file then you have a bigger problem, use an interface or pimpl to hide implementation details.
I've seen a few questions asking this from before C++11 started being implemented by compilers. VS2012 currently implements some C++11 functionality, but has plenty of unimplemented C++11 features. I cannot find mention of whether strings are now guaranteed contiguous or not, and am wondering if they are.
The C++11 guarantee of contiguousness is item 21.4.1.5 of this working draft of the standard.
If possible I would like an official citation with the answer.
Visual C++ has always used contiguous storage for std::string.
One of the design decisions for implementing the contiguity requirement was that all current compilers were doing that anyway. Microsoft is quite well represented on the ISO C++ committee; there's no chance that they overlooked it. Visual C++ did go through a phase of implementing a copy-on-write "optimization" but never was one of the ones using non-contiguous "ropes".
I don't know in detail what the standard says, but std::string has the very interesting method c_str() that returns a const char* to the null-terminated string.
I would be crazy to have strings implemented not using contiguous memory. Especially because c_str() is also const.
So, applying the Occam's razor, we can say that it should be safe. And it has nothing to do with C++11, it's applicable also to the older standards.
I'm working on a wrapper to compile c++ code having MFC and windows API calls into their linux versions.
The c++ code has the following characteristics:
No GUI component present.
Has a maximum of about 10 MFC classes used mostly for string parsing.
It has lots of windows specific constants used such as HINSTANCE, LPCTSTR and so on.
I'm not allowed to compile using wine in linux. Till now i've come across wxwidgets, it seems quite vast, i doubt if i'll be needing all it's components.
Please share your ideas in creating the wrapper, is there any specific code that is already available which does this task or part of this task ?
There is no automatic or even semi-automatic way to convert an MFC application to wxWidgets. It is, of course, perfectly possible to do it and many, many projects have gone through this transformation but you just need to do it.
See MFC page of wxWiki for some starting points.
I have source code for USB communication ("USBThread class") written in Borland C++ and uses Visual Component Library("vcl.h").
Now my task is to port that to Visual C++, because we did not buy Borland C++.
But this "USBThread class" has inheritance from a base class in "vcl.h", called "TThread".
May I ask , in Visual C++, What kind of base class I can use to substitute "TThread' as new inheritance source?
The original code uses "WaitFor" and "Terminate" methods coming TThread,
I do not know how to implement these two menthods with Visual C++.
Thanks!
This is likely to be a difficult exerice imo but it looks like Boost.Thread using join for WaitFor and interrupt for Terminate would get you started.
I am basing this on review of the docs for VCL found here. I say this is likely to be difficult because VCL may have behaviour that is undocumented or otherwise unexpected.
Although you could translate the VCL's TThread class into C++, it would not work very well because it relies on some Delphi semantics that simply do not translate to C++ at all (in particular the TObject::AfterConstruction() method). You are best off simply re-writing USBThread to use Win32 thread functions directly, namely CreateThread() and WaitForSingleObject(). For Terminate(), you simply set a bool flag somewhere that your thread procedure can look at periodically and stop its work when set to true.