3,039 karma · joined Aug 10, 2011 (15y ago) · 383 submissions
comment 6y ago · link · parent
OP says:
> Look at your program's CPU usage. If the sum of your threads is <100% of one core, then there is no reason whatsoever for you to use threads.
Attacking a CPU-bound problem with parallelism is only one reason for using threads. Concurrency (even on one processor) has benefits.
> The shame is in believing ourselves super-human, and reaching for a tool that we don't need, in full knowledge that we're likely to shoot ourselves in the foot with it.
Although synchronization primitives require care, they're not that hard. You don't need super-human powers.
I took some time to understand pthreads and apply them to example programs. For a careful and balanced overview, see my article:
https://begriffs.com/posts/2020-03-23-concurrent-programming...
comment 6y ago · link · parent
* >=8 bits: char (CHAR_BIT is exactly 8 in POSIX)
* >=16 bits: short and int
* >=32 bits: long
* >=64 bits: long long
The C99 typedefs like uint16_t have to be chosen internally to be one of the underlying types. For those sizes that have no matching underlying type, the implementation will omit typedefs.
However don't forget the more flexible C99 typedefs int_leastN_t and int_fastN_t. They both will give you a type of at least N bits, where the "fast" one chooses whichever type is most convenient for the processor, and the "least" version picks whichever is smallest. (For instance int_least16_t is probably short, and int_fast16_t is probably int.)
comment 6y ago · link · parent
Here are examples of working with unicode in C: https://begriffs.com/posts/2019-05-23-unicode-icu.html
comment 6y ago · link · parent
* First call setlocale(LC_CTYPE, "en_US.UTF-8")
* Next feed the UTF-8 string representation of every Unicode codepoint one at a time to mbstowcs() and ensure that the output for each is a wchar_t string of length one
* If all input codepoints numerically match the output wchar_t UTF-32 code units, then the implementation is officially good, and should define __STDC_ISO_10646__?
comment 6y ago · link · parent
For instance, the macOS clang environment does not define this symbol. Is their implementation of wchar_t or <wctype.h> lacking some aspect of Unicode support?
38.
Concurrent Programming, with Examples(begriffs.com ↗)
330 points · by begriffs · 6y ago · 88 comments
comment 6y ago · link · parent
http://iso-9899.info/wiki/Books#Learning_C
Read some and do the exercises. You'll learn to write portable code to a standard rather than stuff that "accidentally works." There's a lot of crap C code in the world, full of GNUisms, overzealous macros, header-only nonsense abusing translation units, and copypasta. Don't pick up bad habits from that stuff.
40.
Ask HN: As a programmer, how to help medicine?
2 points · by begriffs · 6y ago · 1 comment
comment 6y ago · link · parent
PGP is a good way to communicate under the right circumstances. Say I'm writing to someone I trust and have verified their key fingerprint via a secure channel (in person, over the phone, etc). If I were sending them login credentials for a shared site, or bank info for setting up a payment, or even just wanted to block general corporate snooping then what's wrong with PGP? It's "pretty good privacy."
It's nice that I can apply the cryptography on a local machine and then send the result over email. I don't need to sign up for the paranoid chat app du jour.
( In fact PGP is useful for more than signing/encrypting mail, see https://begriffs.com/posts/2016-11-05-advanced-intro-gnupg.h... )
42.
The most evil Lisp macro(nickdrozd.github.io ↗)
3 points · by begriffs · 7y ago · discuss
comment 7y ago · link · parent
I wrote it in reply to an earlier post, where someone "beat C" with Haskell.
comment 7y ago · link · parent
IMHO investing time to get too comfortable with the changing mac ecosystem is ultimately wasted work since it will eventually change/break.
comment 7y ago · link · parent
It's not that people want to pass strncpy source buffers that lack NUL termination, it's that strncpy in certain situations will not NUL terminate its results.
https://begriffs.com/posts/2019-01-19-inside-c-standard-lib....
> some people claim strlcpy() is 'broken'
Speaking of strlcpy, it thankfully doesn't have the problem that strncpy does. However strlcpy is not in the C standard or in POSIX, so can't be used portably. In C99 snprintf is a better choice.
comment 7y ago · link · parent
C is not a "try it and see" language. If you stray from the spec then things will act differently and break between compilers. Section 7.13.2 specifies that only local variables declared as volatile will have a determinate value after a longjmp.
comment 7y ago · link · parent
Outside of the current function, yes. Anywhere, no.
Longjmp allows you to return to places you’ve already been and have saved with setjmp. Also attempting to longjmp to a function that has since returned is undefined behavior. Thus longjmp’s usual use case is like exceptions in other languages, going back up the call chain, skipping intermediate functions.
comment 7y ago · link · parent
* `encoding` is what vim uses internally -- the way buffers and registers are stored in memory. Default value is latin1 as you mention, but it will also pick up on the $LANG environment variable. It's more effective in general to set that variable than directly tweak `encoding`. (Mine is en_US.UTF-8 due to my locale.)
* `fileencoding` is the encoding for the current buffer on disk, how it will be read and written. It is translated in memory to `encoding`. Thus you can manipulate files in encodings other than the one used in-memory. (Although if encoding was latin1 I'm not sure how well the round-trip would go.)
* `termencoding` is how the buffer will be displayed to the screen and how the user input will be read. Once again, it can differ from encoding, and will be translated.
* `fileencodings` is an ordered list of options to try when opening a file. Default value: "ucs-bom,utf-8,default,latin1" when encoding is a unicode value.
If you were editing Japanese text, for instance, choosing UTF-16 encoding would actually make vim use less memory than UTF-8, and would not affect the fileencoding used on disk either.
comment 7y ago · link · parent
http://www.gmarik.info/blog/2014/why-i-stopped-contributing-...
54.
History and Effective Use of Vim(begriffs.com ↗)
593 points · by begriffs · 7y ago · 218 comments
55.
History and Effective Use of Vim(begriffs.com ↗)
2 points · by begriffs · 7y ago · discuss