Question

I am trying to set a unsigned char[32] via a cin loop in main and make it accessible to all threads. The method and delivery is less important than the ability to make this data available to all threads of an unknown number at compile time, cores - 1, whenever the data is reset.

The only ways I can envision this delivery with methods that work as expected are to either

  1. Pass the data to the thread that sends work to the cores - 1 threads from the network then send it from there to those threads. This limits the performance of the worker threads because they are now dependent upon the managing thread because not all work comes from the managing thread, yet the worker threads are dependent upon the up to date unsigned char[32[]
  2. Have each worker thread check for a change in the unsigned char[32] on each cycle. The performance for this is horrible because of lock contention.

I don't believe that I'm able to use an unsigned char[32] with std::atomic; however, if that belief is incorrect, please show me how.

Therefore, I'm investigating if it's possible with boost::atomic, yet I'm having trouble understanding the limitations:

Using non-POD-classes as template parameter to atomic results in undefined behavior: This means that any class containing a constructor, destructor, virtual methods or access control specifications is not a valid argument in C++98. C++11 relaxes this slightly by allowing "trivial" classes containing only empty constructors. Advise: Use only POD types.

I've found this on POD, but I'm still unsure if a unsigned char[32] can be used with boost::atomic safely according to my intent:

The term POD is an acronym. It stands for "plain old data" [p. 5, footnote 4], and is intended to suggest areas of substantive compatibility between comparable data types in C and C++. The terms POD and POD object are often used interchangeably to refer to an object of POD type.

The term POD types collectively refers to the following categories of C++ types, and encompasses both cv-qualified versions of these as well as arrays of these [§3.9, ¶10; §9, ¶4]:

  • scalar types, and
  • POD class types.

Many definitions follow, but I'm still unsure.

Can a unsigned char[32] be used with std::atomic or boost::atomic? If so, how? If not, how can my intent be implemented?

Was it helpful?

Solution

You can use std::atomic<std::array<unsigned char, 32>> instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top