resource-management

What wrapper class in C++ should I use for automated resource management?

安稳与你 提交于 2019-11-29 03:26:16
I'm a C++ amateur. I'm writing some Win32 API code and there are handles and weirdly compositely allocated objects aplenty. So I was wondering - is there some wrapper class that would make resource management easier? For example, when I want to load some data I open a file with CreateFile() and get a HANDLE . When I'm done with it, I should call CloseHandle() on it. But for any reasonably complex loading function there will be dozens of possible exit points, not to mention exceptions. So it would be great if I could wrap the handle in some kind of wrapper class which would automatically call

Are Locks AutoCloseable?

走远了吗. 提交于 2019-11-28 07:11:17
Are Locks AutoCloseable? That is, instead of: Lock someLock = new ReentrantLock(); someLock.lock(); try { // ... } finally { someLock.unlock(); } can I say: try (Lock someLock = new ReentrantLock()) { someLock.lock(); // ... } in Java 7? dlev No, neither the Lock interface (nor the ReentrantLock class) implement the AutoCloseable interface, which is required for use with the new try-with-resource syntax. If you wanted to get this to work, you could write a simple wrapper: public class LockWrapper implements AutoCloseable { private final Lock _lock; public LockWrapper(Lock l) { this._lock = l;

Simple Scala pattern for “using/try-with-resources” (Automatic Resource Management)

六月ゝ 毕业季﹏ 提交于 2019-11-27 22:07:28
C# has using with the IDisposable interface. Java 7+ has identical functionality with try and the AutoCloseable interface. Scala lets you choose your own implementation to this issue. scala-arm seems to be the popular choice, and is maintained by one of the Typesafe employees. However, it seems very complicated for such a simple behavior. To clarify, the usage instructions are simple, but understanding how all that code is working internally is rather complex. I just wrote the following super simple ARM solution: object SimpleARM { def apply[T, Q](c: T {def close(): Unit})(f: (T) => Q): Q = {

RAII in Java… is resource disposal always so ugly?

扶醉桌前 提交于 2019-11-27 20:40:53
I just played with Java file system API, and came down with the following function, used to copy binary files. The original source came from the Web, but I added try/catch/finally clauses to be sure that, should something wrong happen, the Buffer Streams would be closed (and thus, my OS ressources freed) before quiting the function. I trimmed down the function to show the pattern: public static void copyFile(FileOutputStream oDStream, FileInputStream oSStream) throw etc... { BufferedInputStream oSBuffer = new BufferedInputStream(oSStream, 4096); BufferedOutputStream oDBuffer = new

What does “opening a connection” actually mean?

孤街醉人 提交于 2019-11-27 14:07:33
I was trying to explain to someone why database connections implement IDisposable, when I realized I don't really know what "opening a connection" actually mean. So my question is - What does c# practically do when it opens a connection? Thank you. There are actually two classes involved in implementing a connection (actually more, but I'm simplifying). One of these is the IDbConnection implementation ( SQLConnection , NpgsqlConnection , OracleConnection , etc.) that you use in your code. The other is a "real" connection object that is internal to the assembly, and not visible to your code. We

What Automatic Resource Management alternatives exist for Scala?

梦想的初衷 提交于 2019-11-27 09:59:01
I have seen many examples of ARM (automatic resource management) on the web for Scala. It seems to be a rite-of-passage to write one, though most look pretty much like one another. I did see a pretty cool example using continuations, though. At any rate, a lot of that code has flaws of one type or another, so I figured it would be a good idea to have a reference here on Stack Overflow, where we can vote up the most correct and appropriate versions. chengpohi For now Scala 2.13 has finally supported: try with resources by using Using :), Example: val lines: Try[Seq[String]] = Using(new

What is the relationship between the using keyword and the IDisposable interface?

强颜欢笑 提交于 2019-11-27 06:15:32
问题 If I am using the using keyword, do I still have to implement IDisposable ? 回答1: If you use the using statement the enclosed type must already implement IDisposable otherwise the compiler will issue an error. So consider IDisposable implementation to be a prerequisite of using. If you want to use the using statement on your custom class, then you must implement IDisposable for it. However this is kind of backward to do because there's no sense to do so for the sake of it. Only if you have

Are Locks AutoCloseable?

随声附和 提交于 2019-11-27 05:44:06
问题 Are Locks AutoCloseable? That is, instead of: Lock someLock = new ReentrantLock(); someLock.lock(); try { // ... } finally { someLock.unlock(); } can I say: try (Lock someLock = new ReentrantLock()) { someLock.lock(); // ... } in Java 7? 回答1: No, neither the Lock interface (nor the ReentrantLock class) implement the AutoCloseable interface, which is required for use with the new try-with-resource syntax. If you wanted to get this to work, you could write a simple wrapper: public class

Simple Scala pattern for “using/try-with-resources” (Automatic Resource Management)

蹲街弑〆低调 提交于 2019-11-27 04:33:47
问题 C# has using with the IDisposable interface. Java 7+ has identical functionality with try and the AutoCloseable interface. Scala lets you choose your own implementation to this issue. scala-arm seems to be the popular choice, and is maintained by one of the Typesafe employees. However, it seems very complicated for such a simple behavior. To clarify, the usage instructions are simple, but understanding how all that code is working internally is rather complex. I just wrote the following super

Spark on YARN too less vcores used

匆匆过客 提交于 2019-11-27 03:33:49
问题 I'm using Spark in a YARN cluster (HDP 2.4) with the following settings: 1 Masternode 64 GB RAM (50 GB usable) 24 cores (19 cores usable) 5 Slavenodes 64 GB RAM (50 GB usable) each 24 cores (19 cores usable) each YARN settings memory of all containers (of one host): 50 GB minimum container size = 2 GB maximum container size = 50 GB vcores = 19 minimum #vcores/container = 1 maximum #vcores/container = 19 When I run my spark application with the command spark-submit --num-executors 30 -